Extract string text into another strings - java

I got a string like this:
String text = number|name|url||number2|name2|url2
Now I have written a loop
int initialiaze = 0;
for(i = initialize; i > text.length(); i++) {
//do the work
}
In this loop I want to extract number to one string, name to one string, url to one string and if I reach || do a action (e.g insert this three string into db) if this action is done, start again an extract number2, name2 and url2 into string and do a action.
Is this possible? Can you tell me how? I dont get it.

you can use .split() method for strings.
String[] bigParts = myString.split("\\|\\|");
for(String part : bigParts)
{
String[] words = part.split("\\|");
//save to db or what you want
}

for your case
StringTokenizer stPipe = null;
StringTokenizer stDblPipe = null;
String firstPipeElement=null;
stPipe = new StringTokenizer(text, "|");
if (stPipe.hasMoreElements())
{
firstPipeElement= stPipe.nextElement().toString();
.......
if(firstPipeElement.equals("||"))
{
stDblPipe = new StringTokenizer(firstPipeElement , "||");
.....
}
}
hope this helps

Java is not my language, but worth try,
String text = number|name|url||number2|name2|url2
String[] temp;
String[] temp2;
int i ;
temp = text.split("\\|\\|")
for(i=0;i<temp.length();i++){
temp2 = temp[i].split("\\|");
String no = temp2[0];
String name = temp2[1];
String url = temp2[2];
// Do processing with no, name, url
}
I hope, this would help

Related

Spliting String into sections with keywords

I have a String i read from a .txt file with has values in sections seperated like
Text first
[section_name_1]
Text with values pattern1
...
[section_name_2]
Text with values pattern2
I need to split the sections at the section_name_# marks and add those to a String [] (Size of the array is fixed). My Code by now does not make some weird output:
//Code:
public static String[] parseFileToParams(File file)
{
String[] sections= {"[section_name_1]","[section_name_2]","[section_name_3]","[section_name_4]"};
String[] params = new String[sections.length+1];
StringBuilder sb = new StringBuilder();
String decoded = parseFile(file);// Returns the Text from the file
for(int i=0; i< sections.length;i++)
{
params[i]= decoded.split(sections[i])[1];
sb.append(params[i]);
}
return params;
}
//For Test of the output
String[] textArray = BasicOsuParser.parseFileToParams(parseFile);
for(int j = 0; j<textArray.length;j++)
{
sb.append(textArray[j]);
}
String text= sb.toString();
System.out.println (text); //Output: su f form formau fnull
// Obviously not how it should look like
Thanks for help!
Try this:
String[] sections= {"[section_name_1]","[section_name_2]","[section_name_3]","[section_name_4]"};
String textFromFile = "Text first [section_name_1] Text with values pattern1 [section_name_2] Text with values pattern2";
int count = 0;
for(int i = 0; i < sections.length; i++){
if(textFromFile.contains(sections[i])){//Use this to tell how big the parms array will be.
count++;
}
sections[i] = sections[i].replace("[", "\\[").replace("]", "\\]");//Removes the brackets from being delimiters.
}
String[] parms = new String[count+1];//Where the split items will go.
int next = 0;//The next index for the parms array.
for(String sec : sections){
String split[] = textFromFile.split(sec);//Split the file's text by the sec
if(split.length == 2){
parms[next] = split[0];//Adds split to the parms
next++;//Go to the next index for the parms.
textFromFile = split[1];//Remove text which has just been added to the parms.
}
}
parms[next] = textFromFile;//Add any text after the last split.
for(String out : parms){
System.out.println(out);//Output parms.
}
This will do what you have asked and it is commented so you can see how it works.
It's not a good idea use split() only for a one delimiter in text. This method tries to separate the text by given regexp pattern and usually used where there are more than one given delimiter in the text. Also you should screen special symbols in reqexp like '.','[' and so on. read about patterns in java. In your case better use substring() and indexOf():
public static String[] parseFileToParams(File file)
{
String[] sections= {"[section_name_1]","[section_name_2]","[section_name_3]","[section_name_4]"};
String[] params = new String[sections.length+1];
String decoded = parseFile(file);// Returns the Text from the file
int sectionStart = 0;
for (int i = 0; i < sections.length; i++) {
int sectionEnd = decoded.indexOf(sections[i], sectionStart);
params[i] = decoded.substring(sectionStart, sectionEnd);
sectionStart = sectionEnd + sections[i].length();
}
params[sections.length] = decoded.substring(sectionStart, decoded.length());
return params;
}
params[i]= decoded.split(sections[i])[1];
This returns the string after the first appearance of the sections[i] i.e. not just until the section[i+1] but till the end of file.
This loop,
for(int i=0; i< sections.length;i++)
{
params[i]= decoded.split(sections[i])[1];
sb.append(params[i]);
}
return params;
Repeatedly splits decoded into 2 halves, separated by the given section. You then append the entire 2nd half into params.
Example, pretend you wanted to split the string "abcdef" along "a", "b", etc.
You would split along a, and append "bcdef" to params, then split along b, and append "cdef" to params, etc., so you would get "bcdefcdef...f".
I think what you want to do is use real regex as the delimiter, something like params = decoded.split([section_name_.]). Look at http://www.tutorialspoint.com/java/java_string_split.htm and https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
and if you want t

How can i extract a specific item with split function

It's possible to extract a specific item in String with split function
example:
offers/BESTOFFERS/FRTNE/FRPAR/2015-01-05?passengers=STANDARD:1&returnDate=2015-01-12&maxVia=0&withThac=false
i want to extract just returnDate
ouptut why i want:
2015-01-12
OR
i want to extract just passengers
ouptut why i want:
STANDARD:1
If you really need to stick on the split method you could solve it for example like this
String str = "offers/BESTOFFERS/FRTNE/FRPAR/2015-01-05?passengers=STANDARD:1&returnDate=2015-01-12&maxVia=0&withThac=false";
int paramDelim = str.indexOf('?');
String parmeters = str.substring(paramDelim + 1, str.length());
String[] parts = parmeters.split("[&=]");
System.out.println("parts = " + Arrays.toString(parts));
parts contain the paramer names (odd entries) and the values (even entries).
If you don't need to stick on the split method try one of the proposed URL parser solutions.
You can also try the below approach of using HashMap
void populateMap()
{
Map<String, String> myMap = new HashMap<String, String>();
String uri = "offers/BESTOFFERS/FRTNE/FRPAR/2015-01-05?passengers=STANDARD:1&returnDate=2015-01-12&maxVia=0&withThac=false";
int len = uri.indexOf('?');
String input = str.substring(len + 1, uri.length());
for(String retVal : input.split("&")
{
String[] innerRet = retVal.split(":");
myMap.put(innerRet[0],innerRet[1]);
}
}
String retValue (String key)
{
return myMap.get(key);
}
String str = "offers/BESTOFFERS/FRTNE/FRPAR/2015-01-05?passengers=STANDARD:1&returnDate=2015-01-12&maxVia=0&withThac=false";
String returnDate = str.split("&")[1].replaceAll
("returnDate=","").trim();
String passengers= str.split("?")[1].split("&")[0].replaceAll
("passengers=","").trim();

Checking whether the String contains multiple words

I am getting the names as String. How can I display in the following format: If it's single word, I need to display the first character alone. If it's two words, I need to display the first two characters of the word.
John : J
Peter: P
Mathew Rails : MR
Sergy Bein : SB
I cannot use an enum as I am not sure that the list would return the same values all the time. Though they said, it's never going to change.
String name = myString.split('');
topTitle = name[0].subString(0,1);
subTitle = name[1].subString(0,1);
String finalName = topTitle + finalName;
The above code fine, but its not working. I am not getting any exception either.
There are few mistakes in your attempted code.
String#split takes a String as regex.
Return value of String#split is an array of String.
so it should be:
String[] name = myString.split(" ");
or
String[] name = myString.split("\\s+);
You also need to check for # of elements in array first like this to avoid exception:
String topTitle, subTitle;
if (name.length == 2) {
topTitle = name[0].subString(0,1);
subTitle = name[1].subString(0,1);
}
else
topTitle = name.subString(0,1);
The String.split method split a string into an array of strings, based on your regular expression.
This should work:
String[] names = myString.split("\\s+");
String topTitle = names[0].subString(0,1);
String subTitle = names[1].subString(0,1);
String finalName = topTitle + finalName;
First: "name" should be an array.
String[] names = myString.split(" ");
Second: You should use an if function and the length variable to determine the length of a variable.
String initial = "";
if(names.length > 1){
initial = names[0].subString(0,1) + names[1].subString(0,1);
}else{
initial = names[0].subString(0,1);
}
Alternatively you could use a for loop
String initial = "";
for(int i = 0; i < names.length; i++){
initial += names[i].subString(0,1);
}
You were close..
String[] name = myString.split(" ");
String finalName = name[0].charAt(0)+""+(name.length==1?"":name[1].charAt(0));
(name.length==1?"":name[1].charAt(0)) is a ternary operator which would return empty string if length of name array is 1 else it would return 1st character
This will work for you
public static void getString(String str) throws IOException {
String[] strr=str.split(" ");
StringBuilder sb=new StringBuilder();
for(int i=0;i<strr.length;i++){
sb.append(strr[i].charAt(0));
}
System.out.println(sb);
}

How to remove matched words from end of String

I want to remove the following words from end of String ‘PTE’, ‘LTD’, ‘PRIVATE’ and ‘LIMITED’
i tried the code but then i stuck. i tried this
String[] str = {"PTE", "LTD", "PRIVATE", "LIMITED"};
String company = "Basit LTD";
for(int i=0;i<str.length;i++) {
if (company.endsWith(str[i])) {
int position = company.lastIndexOf(str[i]);
company = company.substring(0, position);
}
}
System.out.println(company.replaceAll("\\s",""));
It worked. But suppose the company is Basit LIMITED PRIVATE LTD PTE or Basit LIMITED PRIVATE PTE LTD or any combination of four words in the end. Then the above code just remove the last name i.e., PTE or PRIVATE and so on, and the output is BasitLIMITEDPRIVATELTD.
I want output to be just Basit
How can i do it?
Thanks
---------------Edit---
Please note here the company name is just an example, it is not necessary that it is always the same. may be i have name like
String company = "Masood LIMITED LTD PTE PRIVATE"
or any name that can have the above mentioned words at the end.
Thanks
You can do this in single line. no need to loop through. just use String#replaceAll(regex, str).
company = company.replaceAll("PTE$*?|LTD$*?|PRIVATE$*?|LIMITED$*?","");
If you place the unwanted words in the map it will be ommitted in the resultant string
HashMap map = new HashMap();
map.put("PTE", "");
map.put("LTD", "");
map.put("PRIVATE", "");
map.put("LIMITED", "");
String company = "Basit LTD PRIVATE PTE";
String words[] = company.split(" ");
String resultantStr = "";
for(int k = 0; k < words.length; k++){
if(map.get(words[k]) == null) {
resultantStr += words[k] + " ";
}
}
resultantStr = resultantStr.trim();
System.out.println(" Trimmed String: "+ resultantStr);
If you want to remove these suffixes only at the end of the string, then you could introduce a while loop:
String[] str = {"PTE", "LTD", "PRIVATE", "LIMITED"};
boolean foundSuffix = true;
String company = "Basit LTD";
while (foundSuffix) {
foundSuffix = false;
for(int i=0;i<str.length;i++) {
if (company.endsWith(str[i])) {
foundSuffix = true;
int position = company.lastIndexOf(str[i]);
company = company.substring(0, position);
}
}
}
System.out.println(company.replaceAll("\\s",""));
If you don't mind transforming PTE Basit LIMITED INC to Basit (and also remove the first PTE), then replaceAll should work, as explained by others.
I was trying to do exactly same thing for one of my projects. I wrote this code few days earlier. Now I was exactly trying to find a much better way to do it, that's how I found this Question. But after seeing other answers I decided to share my version of the code.
Collection<String> stopWordSet = Arrays.asList("PTE", "LTD", "PRIVATE", "LIMITED");
String company = "Basit LTD"; //Or Anything
String[] tokens = company.split("[\#\]\\\_\^\[\"\#\ \!\&\'\`\$\%\*\+\(\)\.\/\,\-\;\~\:\}\|\{\?\>\=\<]+");
Stack<String> tokenStack = new Stack<>();
tokenStack.addAll(Arrays.asList(tokens));
while (!tokenStack.isEmpty()) {
String token = tokenStack.peek();
if (stopWordSet.contains(token))
tokenStack.pop();
else
break;
}
String formattedCompanyName = StringUtils.join(tokenStack.toArray());
Try this :
public static void main(String a[]) {
String[] str = {"PTE", "LTD", "PRIVATE", "LIMITED"};
String company = "Basit LIMITED PRIVATE LTD PTE";
for(int i=0;i<str.length;i++) {
company = company.replaceAll(str[i], "");
}
System.out.println(company.replaceAll("\\s",""));
}
All you need is to use trim() and call your function recursively, Or each time you remove a sub string from the end, reset your i to 0.
public class StringMatchRemove {
public static void main(String[] args) {
String str="my name is noorus khan";
String search="noorus";
String newString="";
String word=str.replace(search," ");
StringTokenizer st = new StringTokenizer(word," ");
while(st.hasMoreTokens())
{
newString = newString + st.nextToken() + " ";
}
System.out.println(newString);
}
first using the replace method we get word=my name is ..... khan (Note: here(.) represents the space). Now we should have to remove these spaces for that we are creating a new string adding all the token simply.
Output: my name is khan

Splitting a period-delimited string into multiple strings

I have a string
String x = "Hello.August 27th.Links.page 1";
I am wondering if I can split this string into 4 other strings based on where the period is. For example, the four other strings would be,
String a = "Hello";
String b = "August 27th";
String c = "Links";
String d = "page 1";
As you can see I basically want to extract certain parts of the string out into a new string, the place where it is extracted is based on where the period is which ends the first string and then shows where the 2nd and, etc. strings end.
Thanks in advance!
In android btw
Use String#split (note that it receives a regex as a parameter)
String x = "Hello.August 27th.Links.page 1";
String[] splitted = x.split("\\.");
Yes of course just use:
String[] stringParts = myString.split("\\.")
String x = "Hello.August 27th.Links.page 1"
String []ar=x.split("[.]");
Perhaps you can use StringTokenizer for this requirement. Here is the simple approach:
String x = "Hello.August 27th.Links.page 1";
if (x.contains(".")) {
StringTokenizer stringTokenizer = new StringTokenizer(x, ".");
String[] arrayOfString = new String[stringTokenizer.countTokens()];
int i = 0;
while (stringTokenizer.hasMoreTokens()) {
arrayOfString[i] = stringTokenizer.nextToken();
i++;
}
System.out.println(arrayOfString[0]);
System.out.println(arrayOfString[1]);
System.out.println(arrayOfString[2]);
System.out.println(arrayOfString[3]);
}
You are done. :)

Categories

Resources