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

comparing strings in java

Reply to Message Icon

Original Message
Name: Tony
Date: October 28, 2002 at 08:33:43 Pacific
Subject: comparing strings in java
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.


Report Offensive Message For Removal

Response Number 1
Name: pyro_gray
Date: October 28, 2002 at 08:51:05 Pacific
Subject: comparing strings in java
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 2
Name: java jules
Date: October 28, 2002 at 13:17:00 Pacific
Subject: comparing strings in java
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 3
Name: java jules
Date: October 28, 2002 at 13:18:00 Pacific
Subject: comparing strings in java
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 4
Name: SN
Date: October 28, 2002 at 16:32:12 Pacific
Subject: comparing strings in java
Reply: (edit)

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: comparing strings in java

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software