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:
xxxxxxxxxx
1
string text = "split-this-text";
2
string[] split = text.Split('-');
3
4
string element1 = split[0]; // split
5
string element2 = split[1]; // this
6
string element3 = split[2]; // text
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.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
string text = "split-this-text";
8
string[] split = text.Split('-');
9
10
string element1 = split[0]; // split
11
string element2 = split[1]; // this
12
string element3 = split[2]; // text
13
14
Console.WriteLine(element1);
15
Console.WriteLine(element2);
16
Console.WriteLine(element3);
17
}
18
}
Output:
xxxxxxxxxx
1
split
2
this
3
text