Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have a list of a Sharepoint items: each item has a title, a description and a type. I successfully retrieved it, I called it result. I want to first check if there is any item in result which starts with A then B then C, etc. I will have to do the same for each alphabet character and then if I find a word starting with this character I will have to display the character in bold.

I initially display the characters using this function:

private string generateHeaderScripts(char currentChar)
{
    string headerScriptHtml = "$(document).ready(function() {" +
        "$(\"#myTable" + currentChar.ToString() + "\") " +
        ".tablesorter({widthFixed: true, widgets: ['zebra']})" +
        ".tablesorterPager({container: $(\"#pager" + currentChar.ToString() +"\")}); " +
        "});";
    return headerScriptHtml;
}

How can I check if a word starts with a given character?

like image 881
sara Avatar asked Mar 20 '13 14:03

sara


People also ask

How do you check if a string starts with a character?

Java String startsWith() MethodThe startsWith() method checks whether a string starts with the specified character(s). Tip: Use the endsWith() method to check whether a string ends with the specified character(s).

How do you check string start with specific character enter by the user?

The startsWith() method returns true if a string starts with a specified string. Otherwise it returns false . The startsWith() method is case sensitive. See also the endsWith() method.

How do you check if a string starts with a particular word?

We can use the startsWith() method of String class to check whether a string begins with a specific string or not, it returns a boolean, true or false.

How do I check if a string starts with a specific character in PHP?

To check if string starts with a specific character, use PHP built-in function strpos(). Provide the string and character as arguments to strpos(), and if strpos() returns 0, then we can confirm that the string starts with the specific character, else not.


2 Answers

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.
        }
    }
like image 80
Dmitriy Khaykin Avatar answered Sep 20 '22 17:09

Dmitriy Khaykin


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.

like image 31
uadrive Avatar answered Sep 21 '22 17:09

uadrive