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

String endswith number
string-endswith-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 = "Extinguisher Moss. Eytelia";
        string stringPlants2 = "Coast Polypody 5";

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

        //this line get string end/last character
        char stringLastCharacter = stringPlants.ToCharArray().ElementAt(stringPlants.Length - 1);

        //this line get string end/last character
        char stringLastCharacter2 = stringPlants2.ToCharArray().ElementAt(stringPlants2.Length - 1);

        Boolean result = char.IsNumber(stringLastCharacter);
        Boolean result2 = char.IsNumber(stringLastCharacter2);

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

        Label1.Text += "<br /><br />stringPlants last character: " + stringLastCharacter;
        Label1.Text += "<br />stringPlants2 last character: " + stringLastCharacter2;

        Label1.Text += "<br /><br />stringPlants ends with number? " + result.ToString();
        Label1.Text += "<br />stringPlants2 ends with number? " + result2.ToString();
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string endswith number</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string endswith 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 endswith number"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples