C# take a part of a String

Asked

Viewed 154 times

0

I have a string that in case you have "-" I want to only take what is before the "-"

var value = "12345-485" or "7896"

I tried using: value. Substring(0,value. Index("-"));

If the value has "-" right, now if you don’t have the "-" field returns error.

1 answer

2


You can break the string when you find the dash and take the first part(Dice 0), if there is no dash returns the integer value

var valor = "123-123";
var valorTratado = valor.split('-')[0];

.net fiddle

https://dotnetfiddle.net/k1g9ZB

Browser other questions tagged

You are not signed in. Login or sign up in order to post.