Search code examples
c#.netextractindexof

Extract The Last Part (Section) of a String


I need to extract only the last part of a string after a / character.

I have tried with LastIndexOf, but it fails.

Any solution?


Attempt

var strDiv2 = tbxAff.Substring(tbxAff.IndexOf(" / "), /*value is missing*/ );
dblDiv2 = Convert.ToDouble(strDiv2);`
        

Solution

  • Use the String.Split() function:

    string[] y = tbxAff.Text.Split(new string[] { " / " }, StringSplitOptions.RemoveEmptyEntries);
    

    Then use it like this:

    string strDiv2 = y[1] // Second Part
    dblDiv2 = Convert.ToDouble(strDiv2);