繁体   English   中英

没有 split() 的拆分词

[英]split word without split()

我设法用某个符号拆分给定的单词或句子,现在我想知道是否可以将多个符号作为字符串来执行此操作。 例如,如果我有 ("1+1=2"),并尝试将其拆分为 ("+=")?

我想过将符号声明为字符串,然后将我已经拥有的循环放入另一个循环中,以便针对符号中的每个字符测试单词中的每个字符,但老实说这让我感到困惑。

^-^ 我很感谢您的任何建议。

公共静态无效 splitBySymbol() {

    int i = 0;

    while (i < word.length()) {

      if (word.charAt(i) == symbol) {

          System.out.println( word.substring(0,i)); ;

          word = word.substring(i + 1);

          i = -1;

      }

      i++;
    }


}

如果您只想打印零件,您可以:

public static void splitBySymbol(String word, String symbols)
{
    // Check each char
    for (int i = 0; i < word.length(); i++) {
        // Get the current char and convert to string (for contains())
        String c = Character.toString(word.charAt(i));
        // If it is a delimeter, print a new line
        if (symbols.contains(c)) {
            System.out.println();
        }
        // Otherwise, print the char
        else {
            System.out.print(c);
        }
    }
}

暂无
暂无

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

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