2

The input is XYZ

The String array contains three string i.e.

  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.

3
  • 1
    Have you looked at the endsWith(String suffix) method in String class ? Commented Nov 26, 2012 at 8:28
  • 1
    Your lastIndexOf has to be exactly the length of your input smaller than the length of your test string. In the case of test.alpha.beta.XYZWorld, it will be less than that. Commented Nov 26, 2012 at 8:28
  • Thanks.. never heard this method... My Bad.... it works.. Commented Nov 26, 2012 at 8:31

3 Answers 3

5

There is an endsWith() method in String.

Sign up to request clarification or add additional context in comments.

3 Comments

can we do this?:first change toCharArray say a[] if(a[i]=='X'&&a[i+1]=='y'&&a[i=2]=='z'&&a[i+3]=='\0'){ //end string }
You can do that, but it is ugly.
yeah endsWith() is better :)
2
    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
    }

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.