How to check if a word starts with a given character?

老子叫甜甜 提交于 2019-11-27 14:05:48

To check one value, use:

    string word = "Aword";
    if (word.StartsWith("A")) 
    {
        // do something
    }

You can make a little extension method to pass a list with A, B, and C

    public static bool StartsWithAny(this string source, IEnumerable<string> strings)
    {
        foreach (var valueToCheck in strings)
        {
            if (source.StartsWith(valueToCheck))
            {
                return true;
            }
        }

        return false;
    }

    if (word.StartsWithAny(new List<string>() { "A", "B", "C" })) 
    {
        // do something
    }

AND as a bonus, if you want to know what your string starts with, from a list, and do something based on that value:

    public static bool StartsWithAny(this string source, IEnumerable<string> strings, out string startsWithValue)
    {
        startsWithValue = null;

        foreach (var valueToCheck in strings)
        {
            if (source.StartsWith(valueToCheck))
            {
                startsWithValue = valueToCheck;
                return true;
            }
        }

        return false;
    }

Usage:

    string word = "AWord";
    string startsWithValue;
    if (word.StartsWithAny(new List<string>() { "a", "b", "c" }, out startsWithValue))
    {
        switch (startsWithValue)
        {
            case "A":
                // Do Something
                break;

            // etc.
        }
    }

Assuming the properties you're checking are string types, you can use the String.StartsWith() method.. for example: -

if(item.Title.StartsWith("A"))
{
    //do whatever
}

Rinse and repeat

Try the following below. You can do either StartsWith or Substring 0,1 (first letter)

    if (Word.Substring(0,1) == "A") {
    }

You could do something like this to check for a specific character.

public bool StartsWith(string value, string currentChar) {
   return value.StartsWith(currentChar, true, null);
}

The StartsWith method has an option to ignore the case. The third parameter is to set the culture. If null, it just uses the current culture. With this method, you can loop through your words, run the check and process the word to highlight that first character as needed.

To return the first character in a string, use:

Word.Substring(0,1) //where word is a string

You could implement Regular Expressions. They are quite powerful, but when you design your expression it will actually accomplish a task for you.

For example finding a number, letter, word, and etc. it is quite expressive and flexible.

They have a really great tutorial on them here:

An example of such an expression would be:

string input = "Some additional string to compare against.";
Match match = Regex.Match(input, @"\ba\w*\b", RegexOptions.IgnoreCase);

That would find all the items that start with an "a" no matter the case. You find even utilize Lambda and Linq to make them flow even better.

Hopefully that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!