迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

如何在 Java 中检查一个字符串是否包含子字符串

作者:迹忆客 最近更新:2023/05/16 浏览次数:

本教程介绍了 Java 中如何检查一个字符串是否包含子字符串,并列举了一些示例代码来了解。


在 Java 中检查一个字符串是否包含子字符串的方法

一个字符串是另一个字符串的一部分,称为该字符串对象的子字符串。字符串是一个字符序列,在 Java 中是一个不可改变的对象。让我们来检查子字符串是否存在于给定的字符串中。

在这个例子中,我们使用 String 类的 contains() 方法,如果找到了字符串的子字符串,就返回 true。我们使用 toLowerCase() 方法来检查是否忽略大小写。

public class SimpleTesting {

    public static void main(String[] args) {
        String str = "abracadaabra";
        String subStr = "daa";
        boolean containsStr = str.contains(subStr);
        System.out.println(containsStr);
        
        // Check with string ignore case
        subStr = "Daa";
        containsStr = str.toLowerCase().contains(subStr.toLowerCase());
        System.out.println(containsStr);
        
        subStr = "baa";
        containsStr = str.toLowerCase().contains(subStr.toLowerCase());
        System.out.println(containsStr);
    }
}

输出:

true
true
false

使用 Java 中的 indexOf() 方法查找子字符串

String 类的 indexOf() 方法是用来获取字符串中子字符串的索引,但我们也可以用它来查找子字符串。

在这里,我们检查子字符串的索引是否大于或等于 0,这意味着子字符串被找到了。如果子字符串在字符串中不存在,它将返回 -1

请看下面的例子和输出。

public class SimpleTesting {

    public static void main(String[] args) {
        String str = "abracadaabra";
        String subStr = "daa";
        boolean containsStr = str.indexOf(subStr)>=0;
        System.out.println(containsStr);

        // check with string ignore case
        subStr = "DaA";
        containsStr = str.toLowerCase().indexOf(subStr.toLowerCase())>=0;
        System.out.println(containsStr);
    
        subStr = "aaa";
        containsStr = str.indexOf(subStr)>=0;
        System.out.println(containsStr);
        
    }
}

输出:

true
true
false

在 Java 中使用 Regex 查找子字符串

我们也可以使用 String 对象的 matches() 方法,将 regex 作为参数,并返回 truefalse

public class SimpleTesting {

    public static void main(String[] args) {
        String str = "abracadaabra";
        String subStr = "daa";
        boolean containsStr = str.matches("(?i).*"+subStr+".*");
        System.out.println(containsStr);

        subStr = "daa";
        containsStr = str.matches("(?i).*"+subStr+".*");
        System.out.println(containsStr);
        
    }
}

输出:

true
true

在 Java 中使用 Apache 查找子字符串

如果你使用 Apache Commons 库,那么你可以使用 StringUtils 类中的 containsIgnoreCase() 方法,如果找到子字符串就返回 true,否则返回 false。请看下面的例子。

import org.apache.commons.lang3.StringUtils;

public class SimpleTesting {

    public static void main(String[] args) {
        String str = "abracadaabra";
        String subStr = "daa";
        boolean containsStr = StringUtils.containsIgnoreCase(str, subStr);
        System.out.println(containsStr);
        
        subStr = "DAa";
        containsStr = StringUtils.containsIgnoreCase(str, subStr);
        System.out.println(containsStr);
        
        subStr = "AcWF";
        containsStr = StringUtils.containsIgnoreCase(str, subStr);
        System.out.println(containsStr);
        
    }
}

输出结果:

true
true
false

使用 Java Regex 查找子字符串

Java 提供了一个包含若干类和方法的 regex 包。其中一个类是 Pattern 类,它提供了几个方法,如 compile(), quote(), matcher() 等。这里,我们将使用这些方法来查找子字符串。

import java.util.regex.Pattern;

public class SimpleTesting {

    public static void main(String[] args) {
        String str = "abracadaabra";
        String subStr = "daa";
        boolean containsStr = Pattern.compile(Pattern.quote(subStr), Pattern.CASE_INSENSITIVE).matcher(str).find();
        System.out.println(containsStr);
        
        subStr = "RaCA";
        containsStr = Pattern.compile(Pattern.quote(subStr), Pattern.CASE_INSENSITIVE).matcher(str).find();
        System.out.println(containsStr);
    }
}

输出:

true
true

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

如何从 Pandas 的日期时间列中提取月份和年份

发布时间:2024/04/23 浏览次数:160 分类:Python

我们可以分别使用 dt.year()和 dt.month()方法从 Datetime 列中提取出年和蛾。我们还可以使用 pandas.DatetimeIndex.month 以及 pandas.DatetimeIndex.year 和 strftime()方法提取年份和月份。

如何获取 Pandas DataFrame 的行数

发布时间:2024/04/23 浏览次数:71 分类:Python

本教程介绍如何通过使用 shape,len()来获取 Pandas DataFrame 的行数,以及有多少行元素满足条件。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便