Without using the StringTokenizer, split the string of type StringBuffer into an array of tokens. All day I try something, but it does not work.

  • And what is the reason for such a strange restriction? Study assignment or interview? - VladD
  • Training assignment - thundermind

3 answers 3

StringBuffer , if you look at the sources, stores its buffer as an array of char[] , and not as some kind of array / list of source pieces received after append() , so splitting a StringBuffer into lexemes is not fundamentally different from splitting a string String into lexemes

And here the choice is also quite limited: either use your least favorite StringTokenizer or split() or write something like a parser yourself (I did not check the code - I just wrote it on my knees):

 String s=myStringBuffer.toString(); //исходная строка int start=0, end; ArrayList<String> lexems=new ArrayList<String>; //складываем лексемы сюда String separator=" "; //ваш разделитель - может быть любым do { end=s.indexOf(separator, start); if(end >= 0) { lexems.add(s.substring(start, end); start=end+separator.length(); } else lexems.add(s.substring(start)); } while(end >= 0); 
  • Replaced the Erray List with the String array, but gives an error. I did not replace correctly? Tokens [i] + = (Str.substring (Start, End)); - thundermind
  • tokens[i++]=str.substring(start, end)); and don't break the java naming convention ! - Barmaley
  • Thanks a lot , I'll try now :) - thundermind
  • int i = 0; int start = 0; int end = 0; String[] tokens = new String[100]; StringBuffer str = new StringBuffer("This is a test message"); String separator = " "; do { end = str.indexOf(separator, end); if(end >= 0) { tokens[i++] = (str.substring(start, end)); start = end; } else tokens[i++] = (str.substring(start, end)); } while(end >= 0); System.out.println(tokens[0]); Throws an error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100 at Main.main (Main.java:26) - thundermind
  • Well, actually, I didn't hire a compiler for you: String[] tokens=new String[100]; StringBuffer str=new StringBuffer("This is a test message"); String separator=" "; do { end=str.indexOf(separator, start); if(end >= 0) { tokens[i++]=(str.substring(start, end)); start=end+separator.length(); } else tokens[i++]=(str.substring(start)); System.out.println("Token #"+i+"="+tokens[i-1]); } while(end >= 0); String[] tokens=new String[100]; StringBuffer str=new StringBuffer("This is a test message"); String separator=" "; do { end=str.indexOf(separator, start); if(end >= 0) { tokens[i++]=(str.substring(start, end)); start=end+separator.length(); } else tokens[i++]=(str.substring(start)); System.out.println("Token #"+i+"="+tokens[i-1]); } while(end >= 0); - Barmaley

try it

 StringBuffer s = new StringBuffer("asd zxc 123 sdf"); String[] a = s.toString().split(" ") 
  • 2
    Space is better for split("\\s+") - anber

A bit off topic, but as an alternative you can use Google Guava, then everything is easier:

 List<String> res = Splitter.on(CharMatcher.BREAKING_WHITESPACE).splitToList("asd zxc 123 sdf");