我想查找一個utility
是否在utilities
之一。推薦使用逗號分隔字符串查找單詞的方法?
我有一個JUnit
測試如下
@Test
public void testUtilityInUtilities() {
final String utilities = "Pacific Gas & Electric (PG&E),San Diego Gas & Electric (SDG&E), Salt River Project (SRP),Southern California Edison (SCE)";
final String utility = "San Diego Gas & Electric (SDG&E)";
assertTrue(utilities.contains(utility));
}
這是個好足夠的測試?還是我應該做類似於以下的事情?
String[] splitString = (utilities.split(","));
for (String string : splitString) {
if (string.equals(utility)) {return true;}
}
return false;
推薦哪種方法? 拆分或包含還是別的?