how can I extract a part from string with "-" separated between the characters? for example, I want to extract "123" from "123-4567-9012" or extract "4567" from "123-4567-9012" or even 9012 from the same string
3 Answers
Just split on the - character and later you can access them. Use string.Split(char[])
string str = "123-4567-9012";
string[] arr = str.Split('-');
it will result in:
arr[0] = "123";
arr[1] = "4567";
arr[2] = "9012"
Comments
If you String.Split, you can break a string into separate parts, based on a delimter. For example, using:
var input = "123-4567-9012";
var parts = input.Split('-');
foreach(var part in parts)
Console.WriteLine(part);
Will output:
123
4567
9012
You can do the opposite with String.Join- for example.
var result = String.Join("-", parts);
Console.WriteLine(result);
Will output:
123-4567-9012
Substring, or you canSplitand get them with array..