SiteMap  | About    

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)


startIndex: The index of the start of the substring.

length: The number of characters in the substring.


The specified substring.


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()

        private void button1_Click(object sender, EventArgs e)
            string str = null;
            string retString = null;
            str = "This is substring test";
            retString = str.Substring(8, 9);

When you execute this C# program , it will display "subtring" in the messagebox. (C) 2015    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.