C# Substring
To get Substring of a string in C#, use Substring function on the string.
</>
Copy
string.Substring(int startIndex, int length)
where
startIndex is the index position in the string from which substring starts.
length is the length of the substring.
length is optional. So, if you provide only startIndex, the substring starting from startIndex till the end of given string is returned by the Substring.
Example 1 – C# Sustring – Using startIndex
In the following example, we will find the substring with startIndex.
Program.cs
</>
Copy
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
string str = "HelloWorld";
int startIndex = 4;
string substring = str.Substring(startIndex);
Console.WriteLine("Substring is : "+substring);
}
}
}
Output
PS D:\workspace\csharp\HelloWorld> dotnet run
Substring is : oWorld
