user1588867
user1588867

Reputation: 67

StringTokenizer delimiters for each Character

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?

Upvotes: 2

Views: 15164

Answers (3)

someone
someone

Reputation: 6572

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);
}

Upvotes: 4

Amarnath
Amarnath

Reputation: 8865

You can do some thing like make the string in to a Char array.

char[] simpleArray = sampleString.toCharArray();

This will split the String to a set of characters. So you can do the operations which you have stated above.

Upvotes: 0

CaTalyst.X
CaTalyst.X

Reputation: 1665

Convert the String to an array. There is no delimiter for separating every single character, and it wouldnt make sense to use string tokenizer to do that even if there was.

You can do something like:

 char[] individualChars = someString.toCharArray;

Then iterate through that array like so:

for (char c : individualChars){
    //do something with the chars.
}

Upvotes: 2

Related Questions