Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine TrimStart and TrimEnd for a String

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.

like image 244
Sebastien Avatar asked Feb 17 '23 20:02

Sebastien


2 Answers

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);
    }
like image 76
Mo the Conqueror Avatar answered Feb 19 '23 10:02

Mo the Conqueror


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");
like image 35
Mark Withers Avatar answered Feb 19 '23 10:02

Mark Withers