Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

rob64464

comparing 2 strings (C#)

This topic is 5235 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have to strings: string test1 = "bceasue" //notice bad spelling string test2 = "because" //notice correct spelling. ok, here is the problem. I am trying to detect is test1 has exactly the same letters as test2. How do I do it? Something to do with character arrays? Its just I am making a word game and I need to detect if a jumbled up letter actully makes a word. Thanks, Rob

Share this post


Link to post
Share on other sites
Advertisement
My first instinct is to convert both strings to char arrays with String.ToCharArray(). Then sort both char arrays with Array.Sort() and then compare the char arrays for equality.

Share this post


Link to post
Share on other sites
thank you! Ill see if that works! I managed to get both strings to char arrays! Never new about the sort command though.

Thank You,
Rob

Share this post


Link to post
Share on other sites
If you have to use char arrays for one reason or another, strings ARE char arrays. Just access each character via indexer as you usually would;

test1[0]
test1[1]
.. etc

EDIT:

You can also do a foreach loop;

foreach (char c in test1) {
// ..
}

[edited by - wyrd on March 22, 2004 5:51:01 PM]

Share this post


Link to post
Share on other sites
That will not work because the word 'because' can change to any word in the dictionairy. Is there any command to split a string into characters and compare the 2 to see if they contain exactly the same characters?

[edited by - rob64464 on March 22, 2004 5:58:18 PM]

Share this post


Link to post
Share on other sites
thats what sort is for. Wont that sort it numerically?
So Because becomes abceesu
& Beausce becomes abceesu then you loop at check SR1 == SR2[i]

Share this post


Link to post
Share on other sites
Array.Sort(test1.ToCharArray());
Array.Sort(test2.ToCharArray());
MessageBox.Show(test1.ToString());
MessageBox.Show(test2.ToString());

that does not change the string. It still gives out bceause and because.

Share this post


Link to post
Share on other sites
You''re converting the strings to temporary arrays, sorting the temporary arrays and throwing the temporary arrays away. You need to keep the arrays that you''ve sorted around. ex:

static bool comp(string s1, string s2)
{
char [] a1 = s1.ToCharArray();
char [] a2 = s2.ToCharArray();
Array.Sort(a1);
Array.Sort(a2);
return a1.ToString() == a2.ToString();
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ummm, did you ever try:

if (string1 == string2) {
// same
} else {
// not same
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!