Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split string before hyphen - asp.net c#

Tags:

c#

.net

split

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?

like image 547
Zerotoinfinity Avatar asked Jan 25 '11 15:01

Zerotoinfinity


People also ask

How do you split a string with a hyphen?

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.

How split a string after a specific character in C#?

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);

How do I split a word in C#?

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.


1 Answers

string result = theString.Substring(0, theString.IndexOf("-")).Trim();
like image 158
Tyler Treat Avatar answered Nov 10 '22 01:11

Tyler Treat