StringTokenizer delimiters for each Character

2019-03-01 05:50发布

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?

3条回答
三岁会撩人
2楼-- · 2019-03-01 06:29

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.

查看更多
相关推荐>>
3楼-- · 2019-03-01 06:36

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.
}
查看更多
forever°为你锁心
4楼-- · 2019-03-01 06:43

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);
}
查看更多
登录 后发表回答