文本是否包含子串

does text contains substring

如何检查某个字符串是否包含特定字符串,例如 "ABC72961"。因此,我们搜索以 "ABC" 开头后跟 5 位数字的字符串。我已经实现了一个算法,但我想要它与 "matches" 或其他方式,然后检查这两个解决方案的速度。

您可以像这样使用 String indexOf 命令:

int result = someString.indexOf("ABC72961")

如果没有匹配项,结果将为 -1。

如果有匹配,结果将是匹配开始的索引。

str.contains("ABC72961");

Returns 如果 str 包含字符串则为真。如果不是则为假。

我想你想用的是java.util.regex.Pattern.

Pattern p = Pattern.compile("ABC(\d*)"); 
Matcher m = p.matcher("ABC72961"); 
boolean b = m.matches();

或者如果正好是"ABC"后的5位数字,你可以使用正则表达式ABC(\d{5})

https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#compile(java.lang.String)


另一个解决方案是:

String stringToTest = "ABC72961"
boolean b = stringToTest.contains("ABC");

http://www.tutorialspoint.com/java/lang/string_contains.htm

您可能需要为此使用

^ABC[0-9]{5}$

  • ^ : 字符串的开头
  • ABC : 按字面匹配 ABC(区分大小写)
  • [0-9]{5} :匹配从 0 到 9 的 5x 个数字
  • $ : 字符串结束

并使用String#matches进行测试


Regex101


例子

String regex = "^ABC[0-9]{5}$";
String one = "ABC72961";
String two = "ABC2345";
String three = "AB12345";
String four = "ABABABAB";

System.out.println(one.matches(regex));      // true
System.out.println(two.matches(regex));      // false
System.out.println(three.matches(regex));    // false
System.out.println(four.matches(regex));     // false

编辑

看到您的评论,您希望它也适用于 String one = "textABC72961text"。为此,您应该删除限制字符串的 ^$

.*ABC[0-9]{5}.*


编辑 2

这里是如果你想提取它

if (s.matches(".*ABC[0-9]{5}.*")) {
    Matcher m = Pattern.compile("ABC[0-9]{5}").matcher(s);
    m.find();
    result = m.group();
}
public String getString() {
        String str = extractString();
        return str;
    }

    public boolean exists() {
        return !getString().trim().equals("") ? false : true;
    }

    private List<Integer> getPositionsOfABC() {
        List<Integer> positions = new ArrayList<>();
        int index = text.indexOf("ABC");
        while (index > 0) {
            positions.add(index);
            index = text.indexOf("ABC", index + 1);
        }
        return positions;
    }

    private static boolean isInteger(String str) {
        boolean isValidInteger = false;
        try {
            Integer.parseInteger(str);
            isValidInteger = true;
        } catch (NumberFormatException ex) {
            return isValidInteger;
        }
        return isValidInteger;
    }

    private String extractString() {
        List<Integer> positions = getPositionsOfABC();
        for (Integer position : positions) {
            int index = position.intValue();
            String substring = text.substring(index, index + LENGTH_OF_DIGITS);
            String lastDigits = substring.substring(3, substring.length());
            if (isInteger(lastDigits)) {
                return substring;
            }
        }
        return "";
    }

这是一个简单的代码,无需使用库函数、正则表达式或其他复杂的数据结构即可检查字符串中是否存在子字符串。

class SSC {
public static void main(String[] args) {
    String main_str <-- MAIN STRING
    String sub_str <-- SUBSTRING
    String w; int flag=0;
    for(int i=0;i<=main_str.length()-sub_str.length();i++){
        w="";
        for(int j=0;j<sub_str.length();j++){
            w+=main_str.charAt(i+j);
        }
        if(w.equals(sub_str))
        flag++;
    }
    if(flag>0)
    System.out.print("exists "+flag+" times");
    else
    System.out.print("doesn't exist");  
  }
}

希望对您有所帮助。