c# - How to check whether a string contains a specific character

Check whether a String contains a specific character
The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects.

The following .net c# tutorial code demonstrates how we can check whether a String instance contains a specific character. In this .net c# tutorial code we will determine whether a String instance contains a specific character or not. Here we will check the specific character’s existence within a String instance by using the String Contains() method.

The String Contains(Char) method overload returns a value indicating whether a specified character occurs within this string. The String Contains(char value) method has a required parameter named value. The value parameter is the character to seek.

The String Contains() method returns a Boolean value. This method returns true if the value parameter occurs within this string otherwise it returns false. So using this String Contains(Char) method overload the .net c# developers can determine whether a String instance contains a specific character or not.

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

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
        //this section create a string variable.
        string stringVal = "abcd efgh ijkl mnop";

        Label1.Text = "string..................<br />";
        Label1.Text += stringVal;

        //this line check string contains character 'b' or not.
        Boolean resultb = stringVal.Contains('b');

        //this line check string exists character 'z' or not.
        Boolean resultz = stringVal.Contains('z');

        Label1.Text += "<br /><br />string contains character ('b')?.........";
        Label1.Text += resultb.ToString();

        Label1.Text += "<br /><br />string contains character ('z')?...........";
        Label1.Text += resultz.ToString();
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string contains character</title>  
    <form id="form1" runat="server">  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string contains character
        <hr width="550" align="left" color="Gainsboro" />  
        <br /><br />
            Text="string contains character"  
More c# examples