String find second occurrence
string-find-second-occurrence.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 = "Paper Birch. Red Birch. Bay Laurel. Silver Birch.";
Label1.Text = "string of plants..................<br />";
Label1.Text += plants+"<br />";
//substring to find/search indexes/occurrences in string.
string indexOfSubStringToFind = "Birch";
int index = 0;
int counter = 0;
//find all indexes/occurrences of specified substring in string
while ((index=plants.IndexOf(indexOfSubStringToFind, index)) != -1)
{
index++;
counter++;
//this line check second occurrence of search substring.
if (counter == 2)
{
Label1.Text += "<br />Second occurrence of [Birch] found at index: " + index;
break;
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - string find second occurrence</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string find second occurrence
</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 second occurrence"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>
- How to replace a substring of a string
- How to split a string with specific delimiter to populate an array
- How to join an array elements into a new string
- How to compare two strings
- How to compare two strings by ignoring case
- How to find number of occurrences of a substring in a string
- How to get the first character of a string
- How to remove characters from a string
- How to remove non numeric characters from a string
- How to reverse a string