1

I have string text="camel", and then I want to check if text contains letter "m", so I loop through it and checking it using:

if (text[i].Equals("m")) 

but this never returns me true... why?

3
  • 10
    text[i] is a char, which will never equal a string. You can just do text[i] == 'm' Commented Feb 6, 2013 at 20:20
  • 1
    Why dont you use text.Contains("m")? Commented Feb 6, 2013 at 20:21
  • What do you want to do with this information? maybe it can help us help you. I'm not sure why Contains wouldn't work in this case, unless you tell us otherwise. Commented Feb 6, 2013 at 20:31

5 Answers 5

3

Since you are comparing a character with a string this won't work. Here's some more information on String comparisons

In this case you should use

if(text.Contains("m")) 
Sign up to request clarification or add additional context in comments.

2 Comments

Equals checks whatever the developer defined it to mean in the Equals method of the type. If not overridden it compares the references for classes and the bytes of all fields for structs. operator == does whatever the user defined operator tells it to do, unless there is no user defined operator in which case it compares the references for classes and the bytes of all fields for structs. So the two concepts are exactly the same except that developers can define each method differently.
@Servy you are correct in that it differs if overloaded. I will edit my answer.
2

As mentioned by @MattGreer, you're currently comparing a character and a string. This is because of the delimiter you've chosen for your literal, and because text[i] returns a character from a string rather than a substring of that string.

Please note the difference between using string literal delimiters (quote) and character literal delimiters (apostrophe):

if (text[i].Equals('m')) 

Also, as others have stated, unless there is some reason you want to iterate through each character, String.Contains() would seemingly serve the intended purpose.

Comments

0

You need to find all occurences of a letter in a text as I understand it:

string text = "camel"; string lookup = "M"; int index = 0; while ( (index = text.IndexOf(lookup, index, StringComparison.OrdinalIgnoreCase) != -1) { // You have found what you looked for at position "index". } 

I don't think that you get it any faster than this.

Good luck with your quest.

Comments

0

The answers has been given to you by Kyle C, so this is how you complete the whole process and I'm gonna use winforms as an example:

private void button1_Click(object sender, EventArgs e) { string text = "camel"; if (text.Contains("m") || text.Contains("M"))//also checks for capital M { MessageBox.Show("True"); } } 

Comments

-2

Miraclessss

Use Contains

You're asking if "camel" is the equivalent of "m" -- which it is not.

"camel" contains "m".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.