java 每个字符的 StringTokenizer 定界符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13522948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
StringTokenizer delimiters for each Character
提问by user1588867
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.
我有一个字符串,我应该在课程中使用 StringTokenizer。我已经制定了如何实施该项目的计划,但我找不到任何关于如何为每个字符设置分隔符的参考。
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?
基本上,像“Hippo Campus is a party place”这样的字符串,我需要将每个字符分成标记,然后将它们与一组值进行比较,然后将一个特定的值与另一个值交换。我知道如何做其他所有事情,但是分隔每个字符的分隔符是什么?
回答by someone
If you really want to use StringTokenizer you could use like below
如果你真的想使用 StringTokenizer 你可以像下面这样使用
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.
或者,您甚至可以为此使用 split。你的结果将是每个字符的字符串数组。
String myStr = "Hippo Campus is a party place";
String [] chars = myStr.split("");
for(String str:chars ){
System.out.println(str);
}
回答by CaTalyst.X
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.
}
回答by Amarnath
You can do some thing like make the string in to a Char array.
你可以做一些事情,比如把字符串变成一个 Char 数组。
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.
这会将字符串拆分为一组字符。所以你可以做你上面说的操作。