java java.lang.string_Java Java.lang.String.matches()用法及代码示例

matches()方法有三种变体。本文介绍了所有这些内容,如下所示:1.字符串matches():此方法告诉此字符串是否与给定的正则表达式匹配。以str.matches(regex)形式调用此方法所产生的结果与表达式Pattern.matches(regex,str)完全相同。

用法:

public boolean matches(String regex)

参数

regex: the regular expression to which this string is to be matched.

Return Value

This method returns true if, and only if, this string matches the given regular expression.

// Java code to demonstrate the

// working of matches()

public class Match1 {

public static void main(String args[]) {

// Initializing String

String Str = new String("Welcome to geeksforgeeks");

// Testing if regex is present

System.out.print("Does String contains regex (.*)geeks(.*) ?:" );

System.out.println(Str.matches("(.*)geeks(.*)"));

// Testing if regex is present

System.out.print("Does String contains regex geeks ?:" );

System.out.println(Str.matches("geeks"));

}

}

输出:

Does String contains regex (.*)geeks(.*) ?:true

Does String contains regex geeks ?:false

2.字符串regionMatches()(带有ignoreCase):此方法有两个变体,可用于测试两个字符串区域是否相等。

Syntax

public boolean regionMatches(boolean ignoreCase,

int str_strt,

String other,

int other_strt,

int len)

参数

str_strt:the starting offset of the subregion in this string.

other:the string argument.

other_strt: the starting offset of the subregion in the string argument.

len: the number of characters to compare.

ignoreCase: if true, ignore case when comparing characters.

Return Value

It returns true if the specified subregion of this string

matches the specified subregion of the string argument;

false otherwise.

Whether the matching is exact or case insensitive depends on the ignoreCase argument.

// Java code to demonstrate the

// working of regionmatches()

public class Match2 {

public static void main(String args[]) {

// Initializing String

String Str1 = new String("Welcome to geeksforgeeks");

// Initializing test String

String Str2 = new String("GEEKS");

// Tests whether GEEKS starts in geeksforgeeks starting from pos 11

// and from 0 ( i.e starting in GEEKS) and ignores case

// and compares 5 characters of GEEKS

System.out.print("Checking if GEEKS is in geeksforgeeks( case insensitive ):" );

System.out.println(Str1.regionMatches(true, 11, Str2, 0, 5));

}

}

输出:

Checking if GEEKS is in geeksforgeeks( case insensitive ):true

3.字符串regionMatches():此方法有两个变体,可用于测试两个字符串区域是否相等。

Syntax

public boolean regionMatches(int str_strt,

String other,

int other_strt,

int len)

参数

str_strt:the starting offset of the subregion in this string.

other:the string argument.

other_strt: the starting offset of the subregion in the string argument.

len: the number of characters to compare.

Return Value

It returns true if the specified subregion of this string

matches the specified subregion of the string argument;

false otherwise.

// Java code to demonstrate the

// working of regionmatches()

public class Match3 {

public static void main(String args[]) {

// Initializing String

String Str1 = new String("Welcome to geeksforgeeks");

// Initializing test String

String Str2 = new String("GEEKS");

// Tests whether GEEKS starts in geeksforgeeks starting from pos 11

// and compares 5 characters of GEEKS

System.out.print("Checking if GEEKS is in geeksforgeeks( case sensitive ):" );

System.out.println(Str1.regionMatches(11, Str2, 0, 5));

}

}

输出:

Checking if GEEKS is in geeksforgeeks( case sensitive ):false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值