简体   繁体   English

如何检查字符串是否以精确字符串结尾?

[英]how to check whether the string ends with exact string?

The input is XYZ 输入是XYZ

The String array contains three string ie String数组包含三个字符串ie

  1. test.alpha.beta.XYZWorld test.alpha.beta.XYZWorld
  2. test.gamaa.mu.XYZ test.gamaa.mu.XYZ
  3. test.nu.tera.XYZ test.nu.tera.XYZ

I need the last two result if i provide the input "XYZ". 如果我提供输入“XYZ”,我需要最后两个结果。 Not the test.alpha.beta.XYZWorld. 不是test.alpha.beta.XYZWorld。 if i use lastIndexOf method defined in java.lang.String, obviously it returns 1,2 and 3 result. 如果我使用java.lang.String中定义的lastIndexOf方法,显然它会返回1,2和3的结果。

Please help. 请帮忙。

String有一个endsWith()方法。

check out String.endsWith(suffix) method from String API . String API中检出String.endsWith(suffix)方法。 it returns a boolean value . 它返回一个布尔值

  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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM