for example we have this string:
and want to get only the "my Name" portion of the string, how could I get this simply with substring?
Also, the format in the example will always be the same so I just need to retrieve what is after the first underscore but before the 2nd underscore.
string.Split
will do for this, no need to go into Substring
:
var parts = "hello_my name_is_bob".Split('_');
string name = parts[1] // == "my name";
Or, in a one liner (though I find this less readable):
string name = "hello_my name_is_bob".Split('_')[1];
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