Home Get everything before dot or comma c#
Reply: 3

Get everything before dot or comma c#

Cieja
1#
Cieja Published in 2017-12-07 21:13:09Z

how can I get a substring of everything before dot or comma?

For example:

    string input = "2.1";
int charLocation = text.IndexOf(".", StringComparison.Ordinal);
    string test = input.Substring(0, charLocation );

but what if I have an input = "2,1" ?

I would like to do it in one method, not using twice a substring (once for dot and once for comma)?

Aominè
2#
Aominè Reply to 2017-12-07 21:16:42Z
string test = input.Split(new Char[] { ',', '.' })[0];
Steve Danner
3#
Steve Danner Reply to 2017-12-07 21:17:12Z

This will split the string for either comma or period...

input.Split(',','.');
Nick Dechiara
4#
Nick Dechiara Reply to 2017-12-07 21:18:16Z

Use the IndexOfAny function. It allows you to specify a list of characters to look for, rather than just a single character. You could then make a substring up to the return value of that function.

e.g.

char[] chars = { '.', ',' }
String out = s.Substring(0,s.IndexOfAny(chars));
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304498 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO