Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: compare two strings to see if they end with the same sub-string

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default compare two strings to see if they end with the same sub-string

    Hi,

    I need to write some java code that will compare two strings and if they both end with the same sub-string.

    eg.

    String NAME
    String TEST

    if (the last 7 characters of the string are equal) {

    do something

    }


    any help appreciated!

    thanks


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: compare two strings to see if they end with the same sub-string

    Search the API for THE string function String.endsWith() and compare them(Edith THE equals)
    If you juist want to compare you dan compare substrings like this:
    stringa.substring(stringa.length()-1-7, stringa.length()).equals(stringa.substring(stringa.length()-1-7, stringb.length()))
    Dont droget to check first if the strings arent shorter then 7
    Quote Originally Posted by tuathan View Post
    Hi,

    I need to write some java code that will compare two strings and if they both end with the same sub-string.

    eg.

    String NAME
    String TEST

    if (the last 7 characters of the string are equal) {

    do something

    }


    any help appreciated!

    thanks

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: compare two strings to see if they end with the same sub-string

    Quote Originally Posted by tuathan View Post
    Hi,

    I need to write some java code that will compare two strings and if they both end with the same sub-string.

    eg.

    String NAME
    String TEST

    if (the last 7 characters of the string are equal) {

    do something

    }


    any help appreciated!

    thanks
    Or you can do:
    int len = stringB.length();
    if (len >= 7 &&  stringA.endsWith(stringB.substring(len-7, len)) {
    ....
    }

Similar Threads

  1. image compare
    By ana0429 in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2012, 03:03 AM
  2. [SOLVED] Assign a loop to variable to compare strings
    By norske_lab in forum Loops & Control Statements
    Replies: 3
    Last Post: March 6th, 2012, 02:46 PM
  3. [SOLVED] How to compare a parameter value to a string literal inside EL?
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 28th, 2011, 06:58 PM
  4. Compare method
    By Evelina in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 16th, 2010, 02:07 PM
  5. String + Compare // Might be too easy for ya
    By Jangan in forum Java Theory & Questions
    Replies: 1
    Last Post: October 18th, 2009, 05:40 PM

Tags for this Thread