迹忆客 专注技术分享

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

在 Java 中检查字符串是否包含不区分大小写的子串

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

本教程介绍如何在 Java 中检查或查找字符串是否包含子字符串。

String 是一个字符序列,有时也称为字符数组。在 Java 中,String 是一个处理所有与字符串相关的操作并提供实用方法的类。

本文演示如何在字符串中查找子字符串。

子字符串是字符串的一部分,也是字符串。它可以有一个或多个字符。

不区分大小写的字符串是不关心字母的小写或大写的字符串。让我们通过一些例子来理解。


在 Java 中的字符串中查找不区分大小写的子字符串

在这个例子中,我们使用了 Pattern 类及其 compile()matcher()find() 方法来检查字符串是否包含子字符串。我们使用了 CASE_INSENSITIVE,它返回一个布尔值,truefalse

请参见下面的示例。

import java.util.regex.Pattern;

public class SimpleTesting{
	public static void main(String[] args){
		String str = "Jiyik";
		String strToFind  = "ji";
		System.out.println(str);
		boolean ispresent = Pattern.compile(Pattern.quote(strToFind), Pattern.CASE_INSENSITIVE).matcher(str).find();
		if(ispresent)
			System.out.println("String is present");
		else System.out.println("String not found");
	}
}

输出:

Jiyik
String is present

在 Java 中使用 StringUtils 在字符串中查找不区分大小写的子字符串

使用 Apache 公共库,你可以使用 StringUtils 类及其 containsIgnoreCase() 方法来查找子字符串。请参见下面的示例。

你必须将 Apache 公共 JAR 添加到你的项目中才能执行此代码。

import org.apache.commons.lang3.StringUtils;

public class SimpleTesting{
	public static void main(String[] args){
		String str = "Jiyik";
		String strToFind  = "ji";
		System.out.println(str);
		boolean ispresent = StringUtils.containsIgnoreCase(str, strToFind);
		if(ispresent)
			System.out.println("String is present");
		else System.out.println("String not found");
	}
}

输出:

Jiyik
String is present

在 Java 中使用 contains() 方法查找字符串中不区分大小写的子字符串

在这个例子中,我们使用了 String 类的 contains() 方法,如果子字符串存在则返回 true。我们使用 toLowerCase() 首先将所有字符转换为小写,然后传递给 contains() 方法。

请参见下面的示例。

public class SimpleTesting{
	public static void main(String[] args){
		String str = "Jiyik";
		String strToFind  = "ji";
		System.out.println(str);
		boolean ispresent = str.toLowerCase().contains(strToFind.toLowerCase());
		if(ispresent)
			System.out.println("String is present");
		else System.out.println("String not found");
	}
}

输出:

Jiyik
String is present

在 Java 中使用 matches() 方法查找字符串中不区分大小写的子字符串

在这个例子中,我们使用了 String 类的 matches() 方法,如果子字符串存在则返回 true。它将正则表达式作为参数。

请参见下面的示例。

public class SimpleTesting{
	public static void main(String[] args){
		String str = "Jiyik";
		String strToFind  = "ji";
		System.out.println(str);
		boolean ispresent = str.matches("(?i).*" + strToFind+ ".*");
		if(ispresent)
			System.out.println("String is present");
		else System.out.println("String not found");
	}
}

输出:

Jiyik
String is present

转载请发邮件至 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 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便