Search code examples
c#asp.nettrim

Trim a string before and after certain characters


I've got a really nasty string that looks like this (not actal rendered HTML, but a string):

<div class='isc-content-block' data-contentmanagerid='60dd19b0-e3ba-4629-935c-a2dd00e052b8' data-contentmanagername='Product: B456009805'>456009806</div>

I need to get the value, the number showing up as "456009806". What would be the best way to trim this string to remove the tags so all that is left is the number inside?


Solution

  • No third party dependency with this solution. If you know the HTML format each time and it's not for a very complicated procedure, then use a simple Regex and adjust accordingly.

    Simple solution:

    var result = Regex.Match("<div class='isc-content-block' data-contentmanagerid='60dd19b0-e3ba-4629-935c-a2dd00e052b8' data-contentmanagername='Product: B456009805'>456009806</div>", ">(.*)</.*>");
    
    result.Groups[1].Value
    

    Will give you:

    456009806
    

    UPDATE:

    Also, running some performance tests with Substring, standard Regex and Compiled regex is pretty amazing. Tried my best to get accurate results with the jitter warmup code and using ticks from StopWatch.

    Gist for Linqpad

    Here is a picture of the results:

    Results from Linqpad with /o+ compiler flag enabled