How to use C# string Substring

Substring in C# string Class returns a new string that is a substring of this string. The substring begins at the specified given index and extended up to the given length.

string string.substring(int startIndex,int length)

Parameters:

startIndex: The index of the start of the substring.

length: The number of characters in the substring.

Returns:

The specified substring.

Exceptions:

System.ArgumentOutOfRangeException : the beginIndex or length less than zero, or the begin index + length not within the specified string




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); 
        }
    }
}

When you execute this C# program , it will display "subtring" in the messagebox.

 CSharp String - Related Contents


 CSharp String - Related Programs

.