【发布时间】:2017-08-05 18:24:59
【问题描述】:
我很难找到解决这个问题的方法:
// look we have two strings that have the same ending " day "
String days[] = new String[] {"Sunday", "Monday"};
// but here i want nday to be choosen for Monday
// and day for Sonday
String splDay[] = new String[] {"Mo","Sun","day","nday"};
现在会发生的是,endsWith 方法将选择“day”而不是“nday”
for (String splt: splDay)
{
if (days[0].endsWith(splt))
{
//now it will pick
Toast.makeText(this , splt , Toast.LENGTH_SHORT).show();
break;
}
}
请记住,我正在处理 1000 个拆分的单词,并且我想选择正确的单词来表示原始单词? 有可能吗?
【问题讨论】:
-
您可以按长度对
splDay进行排序,从最长的开始。无法为完整的答案而烦恼,但如果您想尝试,请查看docs.oracle.com/javase/8/docs/api/java/util/…。