ASP.NET - substring in asp.net

Asked By sushma on 05-Sep-11 05:49 AM
hi,

can any one tell how to use substring to break the text.
smr replied to sushma on 05-Sep-11 06:02 AM
HI

try this

using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string str = null;
            string retString = null;
            str = "This is substring test";
            retString = str.Substring(8, 9);
            MessageBox.Show(retString); 
        }
    }
}
Anoop S replied to sushma on 05-Sep-11 06:28 AM
In ASP.Net C# substring string function returns the part of a string starting from the specified starting index. It have two overloads:

1. Substring(int startIndex)

2. Substring(int startIndex, int length)

1st type of Substring overloaded function accepts only single parameter as integer type starting index of the character position in the specified string.

Response.Write("helloworld".Substring(3))

It will show only the loworld. hel will be removed. In this function it will remove the first 3 letters

2nd type Substring overloaded function accepts two types of parameters, first as integer type starting index of the character position in the string and second parameter as the integer type length or the number of characters to be returned as the substring of specified string.

In second type we will write

Response.Write("helloworld".Substring(0, "helloworld".Length - 3));

Now the output will come hellowo

To remove the last character from string

 public string RemoveLastString(string targetString)
{

return targetString.Substring(0,targetString.Length-1);
}


Page_Load()
{
Response.Write( RemoveLastString("HelloWorld"));
}
Reena Jain replied to sushma on 05-Sep-11 06:48 AM
hi,

The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.

This method extracts the characters in a string between "from" and "to", not including "to" itself.


//Example of extract characters from a string:
<script type="text/javascript">
 
var str="Reena Jain!";
document.write(str.substring(5)+"<br />");
document.write(str.substring(5,4));
 
</script>

Hope this will help you