Java String—所有指定字符串的搜索索引

Java String—所有指定字符串的搜索索引,java,regex,string,Java,Regex,String,例如,我有一个字符串 String value1 = "12345 abc 123 def 123"; 还有一个要在上面搜索 String value2 ="123"; 比如说 如何返回第一个字符串上出现的第二个字符串的所有索引?根据我搜索的内容,indexOf和lastIndexOf在字符串中搜索指定字符串的第一个和最后一个索引。在循环中使用。在循环中使用。使用indexOf的可选参数 List<Integer> indexes = new ArrayList<>(

例如,我有一个字符串

String value1 = "12345 abc 123 def 123";
还有一个要在上面搜索

String value2 ="123";
比如说


如何返回第一个字符串上出现的第二个字符串的所有索引?根据我搜索的内容,
indexOf
lastIndexOf
在字符串中搜索指定字符串的第一个和最后一个索引。

在循环中使用。

在循环中使用。

使用indexOf的可选参数

List<Integer> indexes = new ArrayList<>();
for (int idx = haystack.indexOf(needle);
     idx != -1;
     idx = haystack.indexOf(needle, idx + 1)) {
  indexes.add(idx);
}
List index=new ArrayList();
for(int idx=haystack.indexOf(针);
idx!=-1;
idx=干草堆指数(针,idx+1)){
添加索引(idx);
}

使用indexOf的可选参数

List<Integer> indexes = new ArrayList<>();
for (int idx = haystack.indexOf(needle);
     idx != -1;
     idx = haystack.indexOf(needle, idx + 1)) {
  indexes.add(idx);
}
List index=new ArrayList();
for(int idx=haystack.indexOf(针);
idx!=-1;
idx=干草堆指数(针,idx+1)){
添加索引(idx);
}

使用正则表达式更好

class Test {
    public static void main(String[] args) {
        String value1= "12345 abc 123 def 123";
        Pattern pattern = Pattern.compile("123");
        Matcher  matcher = pattern.matcher(value1);

        int count = 0;
        while (matcher.find()){
            count++;
}

        System.out.println(count);   
    }
}

使用正则表达式更好

class Test {
    public static void main(String[] args) {
        String value1= "12345 abc 123 def 123";
        Pattern pattern = Pattern.compile("123");
        Matcher  matcher = pattern.matcher(value1);

        int count = 0;
        while (matcher.find()){
            count++;
}

        System.out.println(count);   
    }
}

您需要自己实现此功能。您可以使用以下内容:

package com.example.stringutils;
import java.util.ArrayList;

public class Util {
/** Return a list of all the indexes of the 'key' string that occur in the 
 * 'arbitrary' string. If there are none an empty list is returned.
 * 
 * @param key
 * @param arbitrary
 * @return
 */
    private static ArrayList<Integer> allIndexesOf(String key, String arbitrary) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        if (key == null |  key.length() == 0 | arbitrary == null | arbitrary.length()<key.length()) {
            return result;
        }

        int loc = -1;
        while ((loc = arbitrary.indexOf(key, loc+1)) > -1) {
            result.add(loc);
        }
        return result;
    }
}
package com.example.stringutils;
导入java.util.ArrayList;
公共类Util{
/**返回“key”字符串中出现的所有索引的列表
*“任意”字符串。如果没有,则返回空列表。
* 
*@param-key
*@param任意
*@返回
*/
私有静态ArrayList allIndexesOf(字符串键,字符串任意){
ArrayList结果=新建ArrayList();
if(key==null | key.length()==0 | arbitral==null | arbitral.length()-1){
结果。添加(loc);
}
返回结果;
}
}

您可能想看看正则表达式是否真的执行得更快(更少的代码行并不总是更快,只是更简单的“代码”)。

您需要自己实现这个函数。您可以使用以下内容:

package com.example.stringutils;
import java.util.ArrayList;

public class Util {
/** Return a list of all the indexes of the 'key' string that occur in the 
 * 'arbitrary' string. If there are none an empty list is returned.
 * 
 * @param key
 * @param arbitrary
 * @return
 */
    private static ArrayList<Integer> allIndexesOf(String key, String arbitrary) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        if (key == null |  key.length() == 0 | arbitrary == null | arbitrary.length()<key.length()) {
            return result;
        }

        int loc = -1;
        while ((loc = arbitrary.indexOf(key, loc+1)) > -1) {
            result.add(loc);
        }
        return result;
    }
}
package com.example.stringutils;
导入java.util.ArrayList;
公共类Util{
/**返回“key”字符串中出现的所有索引的列表
*“任意”字符串。如果没有,则返回空列表。
* 
*@param-key
*@param任意
*@返回
*/
私有静态ArrayList allIndexesOf(字符串键,字符串任意){
ArrayList结果=新建ArrayList();
if(key==null | key.length()==0 | arbitral==null | arbitral.length()-1){
结果。添加(loc);
}
返回结果;
}
}

您可能想看看正则表达式是否真的执行得更快(更少的代码行并不总是更快,只是更简单的“代码”)。

尝试带有
Pattern
Matcher
的正则表达式。 然后可以使用
matcher.start()
获取开始索引,使用
matcher.end()获取结束索引(独占)

这将为您提供以下输出:

Start: 0 -- End: 3
Start: 10 -- End: 13
Start: 18 -- End: 21

尝试使用
模式
匹配器
的正则表达式。 然后可以使用
matcher.start()
获取开始索引,使用
matcher.end()获取结束索引(独占)

这将为您提供以下输出:

Start: 0 -- End: 3
Start: 10 -- End: 13
Start: 18 -- End: 21

正则表达式搜索用于正则表达式,而不是文本字符串。转义输入或预期“regexy”needle出现问题同样,如果是
12121
haystack和
121
needle,regex将只返回1个匹配,而有2个重叠匹配。regex搜索的是正则表达式,而不是文本字符串。逃避输入或预期“regexy”针头出现问题同样,如果出现
12121
haystack和
121
pinder,regex将只返回1个匹配,而有2个重叠匹配。