使用子字符串的数组中元素的索引
Index of a element in array using substring
我需要获取数组中要搜索的元素的索引:
String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q = "Two"; //need to find index of element starting with sub-sting "Two"
我试过的
尝试-1
String temp = "^"+q;
System.out.println(Arrays.asList(items).indexOf(temp));
尝试-2
items[i].matches(temp)
for(int i=0;i<items.length;i++) {
if(items[i].matches(temp)) System.out.println(i);
}
两者都没有按预期工作。
您最好像这样使用 startsWith(String prefix)
:
String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q = "Two"; //need to find index of element starting with substring "Two"
for (int i = 0; i < items.length; i++) {
if (items[i].startsWith(q)) {
System.out.println(i);
}
}
您的第一次尝试没有成功,因为您试图获取列表中字符串 ^Two
的索引,但是 indexOf(String str)
不接受正则表达式。
您的第二次尝试无效,因为 matches(String regex)
对整个字符串有效,而不仅仅是开头。
如果您使用的是 Java 8,您可以编写以下代码,其中 returns 以 "Two"
开头的第一项的索引,或者 -1 如果 none 被发现。
String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q = "Two";
int index = IntStream.range(0, items.length).filter(i -> items[i].startsWith(q)).findFirst().orElse(-1);
我认为您需要为此实施 LinearSearch
,但有一个转折点,您正在搜索 substring
。你可以试试这个。
String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q= "Two"; //need to find index of element starting with sub-sting "Two"
for (int i = 0; 0 < items.length; i++) {
if (items[i].startsWith(q)){
// item found
break;
} else if (i == items.length) {
// item not found
}
}
String q= "Five";String pattern = q+"(.*)";
for(int i=0;i<items.length;i++)
{
if(items[i].matches(pattern))
{
System.out.println(i);
}
}
我需要获取数组中要搜索的元素的索引:
String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q = "Two"; //need to find index of element starting with sub-sting "Two"
我试过的
尝试-1
String temp = "^"+q;
System.out.println(Arrays.asList(items).indexOf(temp));
尝试-2
items[i].matches(temp)
for(int i=0;i<items.length;i++) {
if(items[i].matches(temp)) System.out.println(i);
}
两者都没有按预期工作。
您最好像这样使用 startsWith(String prefix)
:
String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q = "Two"; //need to find index of element starting with substring "Two"
for (int i = 0; i < items.length; i++) {
if (items[i].startsWith(q)) {
System.out.println(i);
}
}
您的第一次尝试没有成功,因为您试图获取列表中字符串 ^Two
的索引,但是 indexOf(String str)
不接受正则表达式。
您的第二次尝试无效,因为 matches(String regex)
对整个字符串有效,而不仅仅是开头。
如果您使用的是 Java 8,您可以编写以下代码,其中 returns 以 "Two"
开头的第一项的索引,或者 -1 如果 none 被发现。
String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q = "Two";
int index = IntStream.range(0, items.length).filter(i -> items[i].startsWith(q)).findFirst().orElse(-1);
我认为您需要为此实施 LinearSearch
,但有一个转折点,您正在搜索 substring
。你可以试试这个。
String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q= "Two"; //need to find index of element starting with sub-sting "Two"
for (int i = 0; 0 < items.length; i++) {
if (items[i].startsWith(q)){
// item found
break;
} else if (i == items.length) {
// item not found
}
}
String q= "Five";String pattern = q+"(.*)";
for(int i=0;i<items.length;i++)
{
if(items[i].matches(pattern))
{
System.out.println(i);
}
}