我有一个字符串数组#1国家/地区[]和另一个具有相同值的字符串数组#2 splitr_str[] 但它分成两半。
我想要的是检查 #2 数组的哪个值与具有完整单词的 #1 字符串数组的前半值匹配。
这是一个例子:
String[] countries = new String[]{"USA", "Spain" ,"Germany"};
String[] split_str = new String[]{"Sp", "ain", "A" ,"US" ,"Ger", "ma","ny"};
现在我只想检测与 ssplit_str 值匹配的国家/地区值的前半部分
对于美国,我只想选择“US”,位于
split_str[3]
如果我找到它了?我选择“A”,即
split_str[2]
等等。
关于如何做到这一点有什么想法吗?
最佳答案
尝试使用 for 循环。正如我刚刚从 this post 了解到的那样,一旦找到匹配项,您就可以使用标签和 break
轻松跳出两个循环。
outerloop:
for( String country : countries){
for( String segment : split_str) {
if( country.startsWith( segment ) ) {
// do something here when the segment has been detected
break outerloop;
}
}
}
关于Java/Android : how to find the first characters of a string if it equals other string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45391713/