Java regionMatches() Method
The regionMatches() method is used to test whether two strings are equal within a region.
Syntax
public boolean regionMatches(int toffset,
String other,
int ooffset,
int len)
或
public boolean regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len)
Parameters
-
ignoreCase -- If true, ignores case when comparing characters.
-
toffset -- The starting offset of the subregion in this string.
-
other -- The string argument.
-
ooffset -- The starting offset of the subregion in the string argument.
-
len -- The number of characters to compare.
Return Value
Returns true if the specified subregion of the string matches the specified subregion of the string argument; otherwise returns false. Whether the match is exact or case-sensitive depends on the ignoreCase parameter.
Example
Example
public
class
Test
{
public
static
void
main
(
String
args
[
]
)
{
String
Str1
=
new
String
(
"www.runoob.com"
)
;
String
Str2
=
new
String
(
"runoob"
)
;
String
Str3
=
new
String
(
"RUNOOB"
)
;
System
.
out
.
(
"Return value :"
)
;
System
.
out
.
println
(
Str1.
regionMatches
(
4
, Str2,
0
,
5
)
)
;
System
.
out
.
(
"Return value :"
)
;
System
.
out
.
println
(
Str1.
regionMatches
(
4
, Str3,
0
,
5
)
)
;
System
.
out
.
(
"Return value :"
)
;
System
.
out
.
println
(
Str1.
regionMatches
(
true
,
4
, Str3,
0
,
5
)
)
;
}
}
}
The execution result of the above program is:
返回值 :true
返回值 :false
返回值 :true
Java String class