Java 在字符串列表中计算笑脸
我试图统计给定Java 在字符串列表中计算笑脸,java,string,list,Java,String,List,我试图统计给定字符串的列表中出现的笑脸次数 笑脸的格式为:或用于眼睛、可选鼻子-或~和嘴巴)或D import java.util .*; public class SmileFaces { public static int countSmileys(List<String> arrow) { int countF = 0; for (String x : arrow) { if (x.charAt(0) ==
字符串的列表中出现的笑脸次数
笑脸的格式为:
或
用于眼睛、可选鼻子-
或~
和嘴巴)
或D
import java.util .*;
public class SmileFaces {
public static int countSmileys(List<String> arrow) {
int countF = 0;
for (String x : arrow) {
if (x.charAt(0) == ';' || x.charAt(0) == ':') {
if (x.charAt(1) == '-' || x.charAt(1) == '~') {
if (x.charAt(2) == ')' || x.charAt(2) == 'D') {
countF++;
} else if (x.charAt(1) == ')' || x.charAt(1) == 'D') {
countF++;
}
}
}
}
return countF;
}
}
import java.util.*;
公共类笑脸{
公共静态int countSmileys(列表箭头){
int countF=0;
用于(字符串x:箭头){
如果(x.charAt(0)=';'| | x.charAt(0)=':'){
如果(x.charAt(1)='-'| | x.charAt(1)='~'){
如果(x.charAt(2)=')'| | x.charAt(2)='D'){
countF++;
}如果(x.charAt(1)=')'| | x.charAt(1)='D'){
countF++;
}
}
}
}
返回计数f;
}
}
最好为此使用regexp表达式。这段小代码使用regexp查找所有模式并报告计数:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.List;
import java.util.Arrays;
// one class needs to have a main() method
public class Test
{
private static final Pattern pattern = Pattern.compile("[:;][-~]?[)D]");
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
List<String> arrow = Arrays.asList(":-) ;~D :) ;D", "sdgsfs :-)");
System.out.println("found: " + countSmileys(arrow));
}
public static int countSmileys(List<String> arrow) {
int count = 0;
for (String x : arrow) {
Matcher matcher = pattern.matcher(x);
while(matcher.find()) {
count++;
}
}
return count;
}
}
import java.util.regex.Pattern;
导入java.util.regex.Matcher;
导入java.util.List;
导入java.util.array;
//一个类需要有一个main()方法
公开课考试
{
private static final Pattern=Pattern.compile(“[:;][-~]?[)D]”);
//使用此编辑器下面的文本字段传递参数
公共静态void main(字符串[]args)
{
列表箭头=数组。asList(“:-);~D:);D”,“sdgsfs:-”;
System.out.println(“找到:+countSmileys(箭头));
}
公共静态int countSmileys(列表箭头){
整数计数=0;
用于(字符串x:箭头){
Matcher-Matcher=pattern.Matcher(x);
while(matcher.find()){
计数++;
}
}
返回计数;
}
}
最佳答案是使用regex
表达式匹配
但因为只有很少的微笑你可以尝试:
@Test
public void countSmiles() {
String smiles = ":),:-D,:~D,:~)";
int count = StringUtils.countMatches(smiles,":)");
count += StringUtils.countMatches(smiles,":D");
count += StringUtils.countMatches(smiles,":-)");
count += StringUtils.countMatches(smiles,":~)");
count += StringUtils.countMatches(smiles,":-D");
count += StringUtils.countMatches(smiles,":~D");
System.out.println("count = " + count);
}
输出:
count = 4
您是否有问题,或者您只是在炫耀您的代码?请提供示例输入数据、期望的结果和观察到的结果。如果字符串大小小于3,这将引发索引AutofBoundsException
;您可能需要为此添加一些验证。另外,也许你应该看看模式
类;它将更优雅地解决问题。countSmileys(Collections.singletonList(“”)
抱歉……它不起作用……测试用例……预期结果得到了我想要的结果,但OP从来没有问过问题,所以我不知道这是否是一个有效的答案嘿,这是一个很好的方法…我不知道为什么我总是从正则表达式转向…谢谢大多数字符串处理都是用正则表达式做的。除了复杂的层次结构,比如JSON或XML或类似的。谢谢……今晚我要坐下来讨论regex——回想一下,我记得几年前我做过这件事,只是稍微了解了一下,它就变得不那么可怕了……谢谢你的帮助……很抱歉我的问题不清楚……我很快就会了解这个网站的窍门……离开它太久了这里有很多在线regexp测试网站。您不需要java来测试它们。在google中搜索“regexp-online”可能会提到这是来自ApacheCommonsLang的