Computing.Net > Forums > Programming > comparing strings in java

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

comparing strings in java

Reply to Message Icon

Name: Tony
Date: October 28, 2002 at 08:33:43 Pacific
OS: xp
CPU/Ram: p3 1.1/256
Comment:

I need a way of comparing two strings x1 and x2 which checks whether x2 can be made by rearranging some of the characters which occur in x1.



Sponsored Link
Ads by Google

Response Number 1
Name: pyro_gray
Date: October 28, 2002 at 08:51:05 Pacific
Reply:

If I was you I would put every letter in x2 into an array then go into a loop and rearrange the letters in the x2 array and then use either .equals() or compareTo() to compare the array x2 to x1 until it either finds a match or get knocked out of the loop.


0

Response Number 2
Name: java jules
Date: October 28, 2002 at 13:17:00 Pacific
Reply:

you could try converting the strings into chars, stick them in two seperate arrays. Then convert into ints, and sort with your fav method. Then u could see if any chars in x2 are in x1


0

Response Number 3
Name: java jules
Date: October 28, 2002 at 13:18:00 Pacific
Reply:

sorry....not sure if this is the best method, so i'm sure anyone with a better idea will be willing to give you more tips


0

Response Number 4
Name: SN
Date: October 28, 2002 at 16:32:12 Pacific
Reply:

Assuming x1 and x2 are the same length, you can just check to see if every character in x1 appears in x2 by using a loop like this one:

for (int i=0; i(lessthan)x1.length; i++)
{
if (x1.indexOf(x2.charAt(i))(lessthan)0)
return false;
}
return true;

Note that the lessthan is spelled out because the sign is interpreted as a tag. Good luck,
SN


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: comparing strings in java

Printing strings in java - Trouble www.computing.net/answers/programming/printing-strings-in-java-trouble/8227.html

sorting an array of string in java www.computing.net/answers/programming/sorting-an-array-of-string-in-java/18011.html

how do you compare 'char' in java www.computing.net/answers/programming/how-do-you-compare-char-in-java/4005.html