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
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:
Code :
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
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
Re: compare two strings to see if they end with the same sub-string
Quote:
Originally Posted by
tuathan
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:
Code java:
int len = stringB.length();
if (len >= 7 && stringA.endsWith(stringB.substring(len-7, len)) {
....
}