Search code examples
c#asp.netstring

Get last characters while character != "_"


I have string like

string test = "http://example.com/upload/user/80_747.jpg"
string test2 = "http://example.com/upload/user/80_4747.jpg"

In both cases i need "747" or "4747". Is there any auto or pre-made function for this or i need to do it fully manual.

ASP.NET C# 4.0


Solution

  • You can split string with _ character and take the last string of result array then again split it with . character and take the first string of result array.

    in JS

    test.split('_')[1].split('.', 1)[0];
    

    in CS use Split, SubString and IndexOf methods of String class.