Java 如何获取多个分隔符的索引?

Java 如何获取多个分隔符的索引?,java,regex,string,indexof,Java,Regex,String,Indexof,我正在寻找一种优雅的方法来查找一组分隔符中的一个的第一次出现 例如,假设我的分隔符集由{;“,”,“/”}组成 如果我的字符串是 “aaa/bbb;ccc)” 我想得到结果3(因为它是第一个出现的,“/”的索引) 如果我的字符串是 “aa;bbbb/” 我想得到结果2(即“;”,因为它是第一个出现的索引) 等等 如果字符串不包含任何分隔符,我想返回-1 我知道我可以先找到每个分隔符的索引,然后计算索引的最小值,而不考虑-1。这段代码变得非常麻烦。我正在寻找一种更简短、更通用的方法。通过正则表达式

我正在寻找一种优雅的方法来查找一组分隔符中的一个的第一次出现

例如,假设我的分隔符集由
{;“,”,“/”}
组成

如果我的字符串是
“aaa/bbb;ccc)”

我想得到结果3(因为它是第一个出现的,
“/”
的索引)

如果我的字符串是
“aa;bbbb/”

我想得到结果2(即
“;”
,因为它是第一个出现的索引)

等等

如果字符串不包含任何分隔符,我想返回
-1


我知道我可以先找到每个分隔符的索引,然后计算索引的最小值,而不考虑
-1
。这段代码变得非常麻烦。我正在寻找一种更简短、更通用的方法。

通过正则表达式,可以这样做

String s =  "aa;bbbb/";
Matcher m = Pattern.compile("[;/)]").matcher(s);   // [;/)] would match a forward slash or semicolon or closing bracket.
if(m.find())                                       // if there is a match found, note that it would find only the first match because we used `if` condition not `while` loop.
{
    System.out.println(m.start());                 // print the index where the match starts.

}
else
{
    System.out.println("-1");                      // else  print -1
}

通过正则表达式,它会这样做

String s =  "aa;bbbb/";
Matcher m = Pattern.compile("[;/)]").matcher(s);   // [;/)] would match a forward slash or semicolon or closing bracket.
if(m.find())                                       // if there is a match found, note that it would find only the first match because we used `if` condition not `while` loop.
{
    System.out.println(m.start());                 // print the index where the match starts.

}
else
{
    System.out.println("-1");                      // else  print -1
}

在输入字符串中每个字符的分隔符列表中搜索。如果找到,则打印索引。
您还可以使用Set来存储分隔符

在输入字符串中每个字符的分隔符列表中搜索。如果找到,则打印索引。
您也可以使用Set来存储下面的分隔符,程序将给出结果。这是使用RegEx完成的

  import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FindIndexUsingRegex {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    findMatches("aaa/bbb;ccc\\)",";|,|\\)|/");
}

public static void findMatches(String source, String regex) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(source);

    while (matcher.find()) {
        System.out.print("First index: " + matcher.start()+"\n");
        System.out.print("Last index: " + matcher.end()+"\n");
        System.out.println("Delimiter: " + matcher.group()+"\n");
        break;
    }
}

}
输出:

   First index: 3
Last index: 4
Delimiter: /

下面的程序将给出结果。这是使用RegEx完成的

  import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FindIndexUsingRegex {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    findMatches("aaa/bbb;ccc\\)",";|,|\\)|/");
}

public static void findMatches(String source, String regex) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(source);

    while (matcher.find()) {
        System.out.print("First index: " + matcher.start()+"\n");
        System.out.print("Last index: " + matcher.end()+"\n");
        System.out.println("Delimiter: " + matcher.group()+"\n");
        break;
    }
}

}
输出:

   First index: 3
Last index: 4
Delimiter: /

与其一个接一个地查找每个分隔符,不如对字符串中的字符进行迭代,并测试每个字符是否为分隔符之一。也可以使用正则表达式。与其逐个查找每个分隔符,不如迭代字符串中的字符,并测试每个字符是否都是分隔符之一,这样效率会更高。或者你可以用正则表达式。