[英]how to check whether the string ends with exact string?
输入是XYZ
String数组包含三个字符串ie
如果我提供输入“XYZ”,我需要最后两个结果。 不是test.alpha.beta.XYZWorld。 如果我使用java.lang.String中定义的lastIndexOf方法,显然它会返回1,2和3的结果。
请帮忙。
String
有一个endsWith()
方法。
从String API中检出String.endsWith(suffix)方法。 它返回一个布尔值 。
String s = "test.gamaa.mu.XYZ";
System.out.println(s.endsWith("XYZ"));
returns TRUE
String pattern = "xyz";
String a = "xyz";
String b = "xyzA";
int position = b.lastIndexOf(pattern);
if (b.length() == position + pattern.length())
{
System.out.print("OK");
} else
{
//error
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.