String find number of occurrences
string-find-number-of-occurrences.aspx
<%@ 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 plants = "Black Alder. False Alder. Striped Alder. White Alder.";
Label1.Text = "string of plants..................<br />";
Label1.Text += plants+"<br />";
//substring to find/search indexes/occurrences in string.
string indexOfSubStringToFind = "Alder";
int index = 0;
int counter = 0;
//find all indexes/occurrences of specified substring in string
while ((index=plants.IndexOf(indexOfSubStringToFind, index)) != -1)
{
index++;
counter++;
}
Label1.Text += "<br />subtring [Alder] found number of occurrrences: " + counter;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - string find number of occurrences</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string find number of occurrences
</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 find number of occurrences"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>
- How to convert a string to int
- How to get a substring from a string
- How to convert a string to uppercase characters
- How to convert a string to lowercase characters
- How to replace a substring of a string
- String contains ignorecase
- How to check whether a string contains any special character
- How to find second occurrence of a substring in a string
- How to get the first character of a string
- How to remove characters from a string