如何删除Java中单个单词后面的所有标点符号? [英] How do I remove all punctuation that follows a single word in Java?
本文介绍了如何删除Java中单个单词后面的所有标点符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要删除一个单词后面的标点符号。例如, word?!
应更改为 word
和 string:
应更改为 string
。
I need to remove punctuation following a word. For example, word?!
should be changed to word
and string:
should be changed to string
.
编辑:算法应该只删除字符串末尾的标点符号。字符串中的任何标点都应该保留。例如,不会;
应该变成不会
。
The algorithm should only remove punctuation at the end of the String. Any punctuation within the String should stay. For instance, doesn't;
should become doesn't
.
推荐答案
您可以使用正则表达式修改字符串。
You could use a regex to modify the string.
String resultString = subjectString.replaceAll("([a-z]+)[?:!.,;]*", "$1");
我所知道的单词'
在最后,它被用作标点符号。所以这个正则表达式适合你。
There are no "words" that I know of where '
is at the end and it is used as a punctuation. So this regex will work for you.
这篇关于如何删除Java中单个单词后面的所有标点符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文