简体   繁体   English

每个字符的 StringTokenizer 定界符

[英]StringTokenizer delimiters for each Character

I've got a string that I'm supposed to use StringTokenizer on for a course.我有一个字符串,我应该在课程中使用 StringTokenizer。 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.基本上,像“Hippo Campus is a party place”这样的字符串,我需要将每个字符分成标记,然后将它们与一组值进行比较,然后将一个特定的值与另一个值交换。 I know how to do everything else, but what the delimiter would be for separating each character?我知道如何做其他所有事情,但是分隔每个字符的分隔符是什么?

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.或者,您甚至可以为此使用 split。 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);
}

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.
}

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.所以你可以做你上面说的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM