EN
C# / .NET - split string by hyphen sign (minus sign - ascii 45 code)
0
points
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