I have a string with some letters and numbers. here an exemple :
OG000134W4.11
I have to trim all the first letters and the first zeros to get this :
134W4.11
I also need to cut the character from the first letter he will encounter to finally retreive :
134
I know I can do this with more than one "trim" but I want to know if there was an efficient way to do that.
Thanks.
If you don't want to use regex.. then Linq is your friend
[Test]
public void TrimTest()
{
var str = "OG000134W4.11";
var ret = str.SkipWhile(x => char.IsLetter(x) || x == '0').TakeWhile(x => !char.IsLetter(x));
Assert.AreEqual("134", ret);
}
Here is the regex I would use
([1-9][0-9]*)[^1-9].*
Here is some C# code you could try
var input = "OG000134W4.11";
var result = new Regex(@"([1-9][0-9]*)[^1-9].*").Replace(input, "$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