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:
string text = "abcd";
string substring = text.Substring(1, 2); // start from index 1 and has 2 characters length
Console.WriteLine(substring); // bc
Practical example
In this example, we present different cases of how to get a substring from the text
.
using System;
public class stringUtils
{
public static void Main(string[] args)
{
string text = "abcd";
string substring1 = text.Substring(0, 1); // a
string substring2 = text.Substring(1, 2); // start from index 1 and has 2 characters length
string substring3 = text.Substring(1); // from index 1 to the end of the string
Console.WriteLine(substring1); // a
Console.WriteLine(substring2); // bc
Console.WriteLine(substring3); // bcd
}
}