[英]Find a specific word in an ArrayList
I am writing a code that loads an ArrayList of Strings and checks to see if a specific word is used. 我正在编写一个代码来加载字符串的ArrayList并检查是否使用了特定的单词。 This may sound weird but the specifics are: 这可能听起来很奇怪,但细节是:
Number of toots containing "toot" (with any capitalization and allowing zero (0) in place of oh (o), eg, "to0t" should be counted too); 包含“toot”的toots数量(任何大小写并允许零(0)代替oh(o),例如“to0t”也应计算在内); however, you should not count a toot if it only contains "toot" as part of another work, eg, "tooter" is not enough to count a toot. 但是,如果它只包含“toot”作为另一项工作的一部分,你不应该算一个嘟嘟声,例如,“tooter”不足以计算一个嘟嘟声。
I have tried many things but it still doesn't seem to work and count the proper results. 我尝试了很多东西,但它似乎仍然无法正常工作并计算出正确的结果。
Here is what I have now after some help from members: 在成员的帮助下,我现在拥有的是:
for (String toot : toots) {
toot = toot.toLowerCase().replace("0", "o").trim();
if(toot.equalsIgnoreCase("toot")){
tootsWithToot++;
}
}
An example would be with the "toot messages" (each line is a separate message): 一个例子是“toot messages”(每行是一个单独的消息):
**1 toot 2 t00t ** 1 toot 2 t00t
toot 嘟
will be back 将会回来
tooter tooting** tooter tooting **
The results should show 2 as tooter and tooting aren't the word "toot" with the O or 0 variations. 结果应该显示2,因为oter和tooting不是具有O或0变化的“嘟嘟”一词。
I think a change should be that I need .contains but I'm not 100% sure. 我认为改变应该是我需要的.contains但我不是100%肯定。 Again, I apologize for this humorous request, I am still a novice programmer. 我再次为这个幽默的请求道歉,我仍然是一个新手程序员。
The following should match your requirements. 以下内容应符合您的要求。 It uses regex to match obnly "whole word" occurrences of your string. 它使用正则表达式来匹配字符串的obnly“整个单词”出现。
for (String toot : toots) {
toot = toot.toLowerCase().replace("0", "o").trim();
toot = toot.toLowerCase().replace("O", "o").trim();
toot = toot.toLowerCase().replace("T", "t").trim();
if(toot.matches(".*\\btoot\\b.*")){
tootsWithToot++;
}
}
A more pure regex example would be something like the following: 更纯粹的正则表达式示例如下所示:
for (String toot : toots) {
if(toot.matches(".*\\b[tT][oO0]{2}[tT]\\b.*")){
tootsWithToot++;
}
}
This regex matches instances of toot
, ignoring the case of the t's [tT]
and the o's (whilst also including 0) [oO0]
, when they are surrounded by any word break character. 此正则表达式匹配的实例toot
,忽略吨的情况下[tT]
和O的(同时也包括0) [oO0]
当它们被任何单词间隔符包围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.