I have a string :
10989898 - test1
or another example:
123178239182 - test2
I need the output like this:
In first case:
10989898
In second case:
123178239182
means the value before hyphen. How can I do that?
Use the split() method to split a string by hyphen, e.g. str. split('-') . The split method takes a separator as a parameter and splits the string by the provided separator, returning an array of substrings.
Split(char[], StringSplitOptions) Method This method is used to splits a string into substrings based on the characters in an array. You can specify whether the substrings include empty array elements. Syntax: public String[] Split(char[] separator, StringSplitOptions option);
The Split() method is part of the string class in C#. The method is used to split a string based on the delimiters passed to the string. The delimiters can be a character, an array of characters, or even an array of strings. We can also pass the function an array of strings to be split on the delimiters passed to it.
string result = theString.Substring(0, theString.IndexOf("-")).Trim();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With