Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Examples – Testing Whether Two String Regions Are Equal

Java Example – Test whether two string regions are equal

Java Examples Java Examples

The following example uses the regionMatches() method to test whether two string regions are equal:

StringRegionMatch.java file

public

class

StringRegionMatch

{

public

static

void

main

(

String

[

]

args

)

{

String

first_str

=

"

Welcome to Microsoft

"

;

String

second_str

=

"

I work with microsoft

"

;

boolean

match1

=

first_str

.

regionMatches

(

11

,

second_str

,

12

,

9

)

;

boolean

match2

=

first_str

.

regionMatches

(

true

,

11

,

second_str

,

12

,

9

)

;

//

The first argument true means ignoring case differences

System

.

out

.

println

(

"

Return value with case sensitivity:

"

match1

)

;

System

.

out

.

println

(

"

Return value ignoring case:

"

match2

)

;

}

}

** first_str.regionMatches(11, second_str, 12, 9)** means comparing the *first_str* string starting from the 11th character "M" with second_str the string starting from the 12th character "M" one by one, comparing 9 pairs of characters in total; since the comparison is case-sensitive, the result is false.

If the first argument is set to true, case differences are ignored, so it returns true.

The output of the above code example is:



区分大小写返回值:false 

不区分大小写返回值:true

Java Examples Java Examples