• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

How should this indexof be?

If I add Andersson I get it right
But if I add aaaaaa its not right

 if (contact[i].LastName.ToLower().IndexOf(letter.ToString().ToLower())== 0)

What I am after is that every word that starts with for example A should be true. But in the case of aaaaa I get the number 4 back in debug mode. How should my if statment be to be like I want to happen?
2 Solutions
If you only care if the name starts with the letter do the following:
 if (contact[i].LastName.ToLower().StartsWith(letter.ToString().ToLower()))

Open in new window

Nate FeinbergRelationship ManagerCommented:
You could just use the StartsWith method, like this:
Hope it helps,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now