String Substring() Method
.net String.Substring method allow us to retrieve a substring from a string object. this method have two
overloaded method those are Substring(Int32) and Substring(Int32, Int32).
Substring(Int32) overloaded method retrieve a substring from a string by starting at a specified character position and continues to the end of the string. this method need to pass a parameter name startIndex. this parameter data type is System.Int32. startIndex parameter value specify a zero based starting character position of the string. this method does not modify the current string instance, it returns a new string that begins at the startIndex position of the current string.
Substring(Int32, Int32) overloaded method allow us to get a substring from a string that start a specified character position of string and has a specified length. this method have two parameters, those are startIndex and length. startIndex is the zero based starting character position of a substring in this instance. length parameter specify the number of characters in the substring.
the folLowing asp.net c# example code demonstrate us how can we get a substring from a string in .net framework.
Substring(Int32) overloaded method retrieve a substring from a string by starting at a specified character position and continues to the end of the string. this method need to pass a parameter name startIndex. this parameter data type is System.Int32. startIndex parameter value specify a zero based starting character position of the string. this method does not modify the current string instance, it returns a new string that begins at the startIndex position of the current string.
Substring(Int32, Int32) overloaded method allow us to get a substring from a string that start a specified character position of string and has a specified length. this method have two parameters, those are startIndex and length. startIndex is the zero based starting character position of a substring in this instance. length parameter specify the number of characters in the substring.
the folLowing asp.net c# example code demonstrate us how can we get a substring from a string in .net framework.
Substring.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void page_Load(object sender, System.EventArgs e) {
if(!this.IsPostBack)
{
TextBox1.Text = "Jones is here.";
}
}
protected void Button1_Click(object sender, System.EventArgs e) {
string myString = TextBox1.Text.ToString();
string subString = myString.Substring(0,5);
Label1.Text = "Substring[0,5]: " + subString;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to get substring from a string in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">asp.net string example: Substring()</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
ForeColor="Firebrick"
Font-Bold="true"
Font-Italic="true"
>
</asp:Label>
<br /><br />
<asp:Label
ID="Label2"
runat="server"
Text="Test String"
ForeColor="Purple"
>
</asp:Label>
<asp:TextBox
ID="TextBox1"
runat="server"
BackColor="Purple"
ForeColor="Snow"
>
</asp:TextBox>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Font-Bold="true"
Text="Get Substring [0,5]"
ForeColor="Purple"
/>
</div>
</form>
</body>
</html>

- How to convert a string to int
- How to convert a string to uppercase characters
- How to convert a string to lowercase characters
- How to join an array elements into a new string
- How to compare two strings
- How to split a string to a list
- How to convert a comma delimited string to array
- How to format a string as a fixed length number
- String format number two digits
- How to format a string as currency