Languages
[Edit]
EN

C# / .NET - split string by hyphen sign (minus sign - ascii 45 code)

0 points
Created by:
Elleanor-Williamson
380

In this article, we would like to show you how to split string by hyphen sign in C#.

Quick solution:

string text = "split-this-text";
string[] split = text.Split('-');

string element1 = split[0]; // split
string element2 = split[1]; // this
string element3 = split[2]; // text

 

Practical example

String Split() function takes as parameter character by which the string will be split into an array of substrings. As a result we get an array. To access an element we just get an element by its index.

using System;

public class Program
{
    public static void Main()
    {
        string text = "split-this-text";
        string[] split = text.Split('-');

        string element1 = split[0]; // split
        string element2 = split[1]; // this
        string element3 = split[2]; // text

        Console.WriteLine(element1);
        Console.WriteLine(element2);
        Console.WriteLine(element3);
    }
}

Output:

split
this
text

References

  1. String.Split Method (System) | Microsoft Docs
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.
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