EN
C# / .NET - get substring from string
0 points
In this article, we would like to show you how to get a substring of the string in C# / .NET.
Quick solution:
xxxxxxxxxx
1
string text = "abcd";
2
string substring = text.Substring(1, 2); // start from index 1 and has 2 characters length
3
Console.WriteLine(substring); // bc
In this example, we present different cases of how to get a substring from the text
.
xxxxxxxxxx
1
using System;
2
3
public class stringUtils
4
{
5
public static void Main(string[] args)
6
{
7
string text = "abcd";
8
9
string substring1 = text.Substring(0, 1); // a
10
string substring2 = text.Substring(1, 2); // start from index 1 and has 2 characters length
11
string substring3 = text.Substring(1); // from index 1 to the end of the string
12
13
Console.WriteLine(substring1); // a
14
Console.WriteLine(substring2); // bc
15
Console.WriteLine(substring3); // bcd
16
}
17
}