[英]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.