简体   繁体   中英

how to check whether the string ends with exact string?

The input is XYZ

The String array contains three string ie

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

I need the last two result if i provide the input "XYZ". Not the test.alpha.beta.XYZWorld. if i use lastIndexOf method defined in java.lang.String, obviously it returns 1,2 and 3 result.

Please help.

String有一个endsWith()方法。

check out String.endsWith(suffix) method from String API . 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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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