StringTokenizer delimiters for each Character

user1588867 picture user1588867 · Nov 23, 2012 · Viewed 12.7k times · Source

I've got a string that I'm supposed to use StringTokenizer on for a course. I've got my plan on how to implement the project, but I cannot find any reference as to how I will make the delimiter each character.

Basically, a String such as "Hippo Campus is a party place" I need to divide into tokens for each character and then compare them to a set of values and swap out a particular one with another. I know how to do everything else, but what the delimiter would be for separating each character?

Answer

someone picture someone · Nov 23, 2012

If you really want to use StringTokenizer you could use like below

     String myStr = "Hippo Campus is a party place".replaceAll("", " ");
    StringTokenizer tokens = new StringTokenizer(myStr," ");

Or even you can use split for this. And your result will be String array with each character.

String myStr = "Hippo Campus is a party place";
String [] chars = myStr.split("");

for(String str:chars ){
  System.out.println(str);
}