c# - How to check whether a string starts with number

String starts with number
the following c# example code demonstrate us how can we check whether a string starts with number. in this example code, at first we convert the string to a char array using string ToCharArray() method. after creating a char array from string, we take the first value of character array using ElementAt() method. at last we test the first char that it is a digit or not using char IsDigit method.

TocharArray() method copy the characters of a string to a Unicode character array. ElementAt() method returns the element at a specified index in a sequence. Char IsDigit() method indicates whether a Unicode character is categorized as a decimal digit.

we also can directly determine the string starts with a number by Char.IsDigit(String, Int32) method. here string is the specified string and index value is (0) zero for string first character.
string-starts-with-number.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        //this section create string variables.
        string stringPlants = "Winter Gilliflower. Bristly Groundberry";
        string stringPlants2 = "1. Yellow Harlequin 2. Spotted Deadnettle 3. Meadow Holly";

        Label1.Text = "string of plants..................<br />";
        Label1.Text += stringPlants;
        Label1.Text += "<br />"+stringPlants2;

        //this line get string first/begin character
        char stringFirstCharacter = stringPlants.ToCharArray().ElementAt(0);

        //this line get string first/begin character
        char stringFirstCharacter2 = stringPlants2.ToCharArray().ElementAt(0);

        Boolean result = char.IsNumber(stringFirstCharacter);
        Boolean result2 = char.IsNumber(stringFirstCharacter2);

        //another option to test character number or letter
        //Boolean result = char.IsDigit(stringFirstCharacter);
        //Boolean result2 = char.IsDigit(stringFirstCharacter2);

        Label1.Text += "<br /><br />stringPlants first character: " + stringFirstCharacter;
        Label1.Text += "<br />stringPlants2 first character: " + stringFirstCharacter2;

        Label1.Text += "<br /><br />stringPlants starts with number? " + result.ToString();
        Label1.Text += "<br />stringPlants2 starts with number? " + result2.ToString();
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string starts with number</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string starts with number
        </h2>  
        <hr width="550" align="left" color="Gainsboro" />  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="string starts with number"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples