Languages
[Edit]
EN

C# / .NET - get substring from string

0 points
Created by:
Theodora-Battle
398

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
    }
}
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# / .NET - String (popular problems)

C# / .NET - get substring from string
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join