問題描述
我正在嘗試使用 Java 中的用戶輸入來獲取一個句子,我需要將其設為小寫并刪除所有標點符號.這是我的代碼:
I am trying to get a sentence using input from the user in Java, and i need to make it lowercase and remove all punctuation. Here is my code:
String[] words = instring.split("\s+");
for (int i = 0; i < words.length; i++) {
words[i] = words[i].toLowerCase();
}
String[] wordsout = new String[50];
Arrays.fill(wordsout,"");
int e = 0;
for (int i = 0; i < words.length; i++) {
if (words[i] != "") {
wordsout[e] = words[e];
wordsout[e] = wordsout[e].replaceAll(" ", "");
e++;
}
}
return wordsout;
我似乎找不到任何方法來刪除所有非字母字符.我嘗試過使用正則表達式和迭代器,但沒有成功.感謝您的幫助.
I cant seem to find any way to remove all non-letter characters. I have tried using regexes and iterators with no luck. Thanks for any help.
推薦答案
這首先刪除所有非字母字符,折疊為小寫,然后拆分輸入,在一行中完成所有工作:
This first removes all non-letter characters, folds to lowercase, then splits the input, doing all the work in a single line:
String[] words = instring.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\s+");
空格最初留在輸入中,因此拆分仍然有效.
Spaces are initially left in the input so the split will still work.
通過在拆分之前刪除垃圾字符,您可以避免遍歷元素.
By removing the rubbish characters before splitting, you avoid having to loop through the elements.
這篇關于如何從 Java 中的輸入文本中刪除標點符號?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!