在Java中将句子字符串转换为单词的字符串数组
我需要我的 Java 程序采用如下字符串:
"This is a sample sentence."
并将其转换为字符串数组,如:
{"this","is","a","sample","sentence"}
没有句点或标点符号(最好)。顺便说一句,字符串输入始终是一个句子。
有没有一种我没有看到的简单方法可以做到这一点?或者我们真的必须大量搜索空格并从空格之间的区域(即单词)创建新字符串吗?
I need my Java program to take a string like:
"This is a sample sentence."
and turn it into a string array like:
{"this","is","a","sample","sentence"}
No periods, or punctuation (preferably). By the way, the string input is always one sentence.
Is there an easy way to do this that I'm not seeing? Or do we really have to search for spaces a lot and create new strings from the areas between the spaces (which are words)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
String.split( ) 会做你想做的大部分事情。然后,您可能需要循环遍历单词以提取所有标点符号。
例如:
String.split() will do most of what you want. You may then need to loop over the words to pull out any punctuation.
For example:
现在,只需使用
split
即可完成,因为它需要正则表达式:这将给出单词:
{"this","is","a","sample","sentence" , "s"}
\\W+
将匹配出现一次或多次的所有非字母字符。所以没有必要更换。您也可以检查其他模式。Now, this can be accomplished just with
split
as it takes regex:this will give words as:
{"this","is","a","sample","sentence", "s"}
The
\\W+
will match all non-alphabetic characters occurring one or more times. So there is no need to replace. You can check other patterns also.您可以使用
BreakIterator .getWordInstance
查找字符串中的所有单词。测试:
输出:
You can use
BreakIterator.getWordInstance
to find all words in a string.Test:
Ouput:
您还可以使用
BreakIterator.getWordInstance
。You can also use
BreakIterator.getWordInstance
.尝试使用以下命令:
这将使用空格作为分割点在字符串数组的每个索引处创建一个子字符串。
Try using the following:
That will create a substring at each index of the array of strings using the space as a split point.
您可以使用此正则表达式来分割字符串
You can just split your string like that using this regular expression
我能想到的最简单和最好的答案是使用在 java 字符串上定义的以下方法 -
只需执行“这是一个示例句子”.split(“”)。因为它需要正则表达式,所以您还可以进行更复杂的分割,其中可能包括删除不需要的标点符号和其他此类字符。
The easiest and best answer I can think of is to use the following method defined on the java string -
And just do "This is a sample sentence".split(" "). Because it takes a regex, you can do more complicated splits as well, which can include removing unwanted punctuation and other such characters.
使用 string.replace(".", "").replace(",", "").replace("?", "").replace("!","").split(' ' ) 将代码拆分为一个不带句点、逗号、问号或感叹号的数组。您可以根据需要添加/删除任意数量的替换调用。
Use
string.replace(".", "").replace(",", "").replace("?", "").replace("!","").split(' ')
to split your code into an array with no periods, commas, question marks, or exclamation marks. You can add/remove as many replace calls as you want.试试这个:
Try this:
我已经在某处发布了这个答案,我会在这里再次发布。此版本不使用任何主要的内置方法。 你得到了字符数组,将其转换为字符串。希望它有帮助!
I already did post this answer somewhere, i will do it here again. This version doesn't use any major inbuilt method. You got the char array, convert it into a String. Hope it helps!
string.replaceAll() 无法正确处理与预定义不同的区域设置。至少在jdk7u10中是这样。
此示例从文本文件创建一个带有 Windows 西里尔字符集 CP1251 的单词词典
string.replaceAll() doesn't correctly work with locale different from predefined. At least in jdk7u10.
This example creates a word dictionary from textfile with windows cyrillic charset CP1251
以下是一个代码片段,它将句子拆分为单词并给出其计数。
Following is a code snippet which splits a sentense to word and give its count too.
另一种方法是 StringTokenizer。
前任:-
Another way to do that is StringTokenizer.
ex:-
这里的大多数答案都按照问题的要求将字符串转换为字符串数组。但一般我们使用 List ,所以更有用的是 -
Most of the answers here convert String to String Array as the question asked. But Generally we use List , so more useful will be -
您可以使用以下简单的代码
You can use simple following code
这是一个简单的 C++ 代码解决方案,没有花哨的函数,使用 DMA 分配动态字符串数组,并将数据放入数组中,直到找到空位。
请参考下面的代码和注释。
我希望它有帮助。
Here is a solution in plain and simple C++ code with no fancy function, use DMA to allocate a dynamic string array, and put data in array till you find a open space.
please refer code below with comments.
I hope it helps.
这应该会有所帮助,
这将创建一个数组,其中元素作为由“”分隔的字符串。
This should help,
this will make an array with elements as the string separated by " ".
试试这个....
TRY THIS....