从另一个字符串数组中过滤字符串数组

发布于 2024-11-04 01:38:36 字数 260 浏览 1 评论 0原文

我对字符串函数有点困惑 我有一个字符串数组 示例

String Array1[]=new String[10];
Array1[0]="Hello";

String Array2[]=new String[10];
Array2[0]="Hello my world";
Array2[1]="Hi buddy";

我想从 Array2 中过滤字符串 Array1[0] 也就是说,字符串“Hello”出现在Array2 的哪个索引中。

i m little bit confused related to string function
I have one array of string
Example

String Array1[]=new String[10];
Array1[0]="Hello";

String Array2[]=new String[10];
Array2[0]="Hello my world";
Array2[1]="Hi buddy";

I want to filter the string Array1[0] from Array2
That is, in which index of Array2 the string "Hello" appears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不美如何 2024-11-11 01:38:36
   /**
     * This method searches for key in the array of String , starts looking from fromIndexSpecified
     * @param source
     * @param key
     * @param fromIndex
     * @return
     */
    private static int findIndexOfKey(String[] source, String key, int fromIndex) {

        if (fromIndex > source.length) {
            return -1;// invalid arg ..can also throw IlligleArgumentException
        }
        for (int index = fromIndex; index < source.length; index++) {
            if (source[index].indexOf(key) > -1) {
                return index;//found..!
            }
        }
        return -1; //no match found
    }
   /**
     * This method searches for key in the array of String , starts looking from fromIndexSpecified
     * @param source
     * @param key
     * @param fromIndex
     * @return
     */
    private static int findIndexOfKey(String[] source, String key, int fromIndex) {

        if (fromIndex > source.length) {
            return -1;// invalid arg ..can also throw IlligleArgumentException
        }
        for (int index = fromIndex; index < source.length; index++) {
            if (source[index].indexOf(key) > -1) {
                return index;//found..!
            }
        }
        return -1; //no match found
    }
下雨或天晴 2024-11-11 01:38:36

您需要循环遍历 Array2 中的项目并进行子字符串比较:

private int findIndexOfSubstring(array, target) {
   for (int i=0; i<array.size(); i++) {
      if array[i].indexOf(target)>-1 return i; //element of Array2 contains target in it.
   }
   return -1; //no match found
}

//somewhere else
String Array1[]=new String[10];
Array1[0]="Hello";

String Array2[]=new String[10];
Array2[0]="Hello my world";
Array2[1]="Hi buddy";

int answer = findIndexOfSubstring(Array2, Array1[0]);

You would need to loop through the items in Array2 and do a substring comparison:

private int findIndexOfSubstring(array, target) {
   for (int i=0; i<array.size(); i++) {
      if array[i].indexOf(target)>-1 return i; //element of Array2 contains target in it.
   }
   return -1; //no match found
}

//somewhere else
String Array1[]=new String[10];
Array1[0]="Hello";

String Array2[]=new String[10];
Array2[0]="Hello my world";
Array2[1]="Hi buddy";

int answer = findIndexOfSubstring(Array2, Array1[0]);
烏雲後面有陽光 2024-11-11 01:38:36

String[] 实际上是一个字符串数组。所以它可以容纳多个字符串。
例如:

String[] stringArray = new String[2];
stringArray[0] = "My String 1";
stringArray[1] = "My String 2";

如果您想找出包含子字符串的字符串数组的第一个索引,您需要执行以下操作:

   int findFirst(String subString, String[] stringArray) {
        for (int i = 0; i < stringArray.length; i++) {
            if (stringArray[i].contains(subString)) {
                return i;
            }
        }
        // indicates that no String in the array contained the subString
        return -1;
    }

以下是使用该方法的方法:

int firstIndex = findFirst("String 2", stringArray);

A String[] is actually an Array of Strings. So it can hold multiples Strings.
For example:

String[] stringArray = new String[2];
stringArray[0] = "My String 1";
stringArray[1] = "My String 2";

If you want to find out the first index of an String array that contains a substring you need to do something like:

   int findFirst(String subString, String[] stringArray) {
        for (int i = 0; i < stringArray.length; i++) {
            if (stringArray[i].contains(subString)) {
                return i;
            }
        }
        // indicates that no String in the array contained the subString
        return -1;
    }

And here is the way to use the method:

int firstIndex = findFirst("String 2", stringArray);
梦里泪两行 2024-11-11 01:38:36

迭代第二个数组,每次检查:

for(int i = 0; i < Array2.length; i++) {
if(Array2[i].contains(Array1[0]) {
 //your logic here
System.out.println("Found match at position " + i);

}
}

编辑:确保在给定的情况下可能会发生空指针异常。也许添加

if(Array2[i] != null)

Iterate over the second array, check every time:

for(int i = 0; i < Array2.length; i++) {
if(Array2[i].contains(Array1[0]) {
 //your logic here
System.out.println("Found match at position " + i);

}
}

Edit: be sure that in your given case Null-Pointer-Exceptions can occur. Perhaps add

if(Array2[i] != null)
山川志 2024-11-11 01:38:36

您可以使用 Apache Commons ArrayUtils 类。

用法:

String[] array1 = new String[10];
array1[0] = "Hello";

String[] array2 = new String[10];
array2[0] = "Hello my world";
array2[1] = "Hi buddy";

int index = ArrayUtils.indexOf(array2, array1[0]);

You can use the method indexOf from Apache Commons ArrayUtils class.

Usage:

String[] array1 = new String[10];
array1[0] = "Hello";

String[] array2 = new String[10];
array2[0] = "Hello my world";
array2[1] = "Hi buddy";

int index = ArrayUtils.indexOf(array2, array1[0]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文