如何从Java输入文本中删除标点符号? [英] How can I remove punctuation from input text in Java?

查看:191
本文介绍了如何从Java输入文本中删除标点符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java中的用户输入来获得一个句子,并且我需要将它变成小写字母并删除所有的标点符号。这是我的代码:

  String [] words = instring.split(\\s +); 
for(int i = 0; i words [i] = words [i] .toLowerCase();
}
String [] wordsout = new String [50];
Arrays.fill(wordsout,);
int e = 0;
for(int i = 0; i if(words [i]!=){
wordsout [e] = words [e] ;
wordsout [e] = wordsout [e] .replaceAll(,);
e ++;
}
}
return wordsout;

我似乎找不到任何方法来删除所有非字母字符。我曾尝试使用正则表达式和迭代器没有运气。感谢您的帮助。

解决方案

这首先删除所有非字母字符,折叠为小写,然后拆分输入,
$ b

  String [] words = instring.replaceAll([^ a-zA-Z] ,).toLowerCase()。split(\\s +); 

空格最初留在输入中,所以分割仍然有效。 b

通过在分割之前删除垃圾字符,可避免必须遍历元素。


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输入文本中删除标点符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆