2

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.

shift66
  • 11,760
  • 13
  • 50
  • 83
Leandros
  • 16,805
  • 9
  • 69
  • 108
  • 2
    take a look at the StringTokenizer http://stackoverflow.com/questions/2356251/string-tokenizer-in-java and http://www.java-samples.com/showtutorial.php?tutorialid=236 – Sergey Benner Jan 24 '12 at 19:01
  • Are the `|` dividers literally there, or are you showing that just to illustrate the division? – Dallas Jan 24 '12 at 19:02
  • I added the dividers, because I thought they would be helpful to split the string. :) – Leandros Jan 24 '12 at 19:13

3 Answers3

6

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
}
shift66
  • 11,760
  • 13
  • 50
  • 83
  • Would there be a reason to go with one (.split() vs. tokenizer) over the other? – Dallas Jan 24 '12 at 19:11
  • I've never used tokenizer so I cant answer your question. But answer of Sergey Benner looks complicated. – shift66 Jan 24 '12 at 19:15
  • Does this method go so long through the string till it ends? – Leandros Jan 24 '12 at 19:24
  • `words` is a StringArray, am I right? How can I log (e.g via `Log.D`) or print the Array with a textView (e.g via `setText`)? Only to test if its work :) – Leandros Jan 24 '12 at 19:51
  • 1
    for(String w : words) { textView.setText(w); Thread.sleep(2000); } after this you'll see in your textView each word for 2 seconds. – shift66 Jan 24 '12 at 19:58
  • Its only your split code, the string and the setText code. Everything on my onCreate method. very simple. – Leandros Jan 24 '12 at 20:24
  • Of course you'll get the last one if you're writing this in onCreate method because component is not visible until onCreate is not completed. You have to make say a button and do this in that buttons onClick event. – shift66 Jan 24 '12 at 20:37
  • 1
    @Leandros I'm not your debugger :D try to debug your application. You asked a question how to split, I answered. Then think yourself. Nothing personal ;-) – shift66 Jan 24 '12 at 20:54
3

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

Sergey Benner
  • 4,421
  • 2
  • 22
  • 29
1

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

RanRag
  • 48,359
  • 38
  • 114
  • 167
Jashwant
  • 28,410
  • 16
  • 70
  • 105
  • edit your answer and escape '|' characters. And already change single quotes to double ('||' --> "\\|\\|") – shift66 Jan 24 '12 at 19:12
  • Thanks @Ademiban for pointing it out and Ranrag for editing. Just out of curiousity ( I dont know java ), wont it run without escaping ? Also if it would be a single character does java will it have single quotes or double quotes ? Because, in C#, text.Split('|') is okay and it does not have any splitting for strings. – Jashwant Jan 25 '12 at 03:05
  • Now your answer is right :) No, in Java there are only two split methods and both takes as argument a text, not a single symbol, so you have to put your text into double quotes even if there is only one symbol. See this http://docs.oracle.com/javase/6/docs/api/java/lang/String.html – shift66 Jan 25 '12 at 05:54