Search code examples
c#special-characterstexttrimming

Trimming degree symbol on C#


Can anyone tell me why this is not working:

string txt = "+0°1,0'";
string degree = txt.TrimEnd('°');

I am trying to separate the degrees on this string, but after this, what remains on degree is the same content of txt.

I am using C# in Visual Studio.


Solution

  • string.TrimEnd remove char at the end. In your example, '°' isn't at the end.

    For example :

    string txt = "+0°°°°";
    string degree = txt.TrimEnd('°');
    // degree => "+0"
    

    If you want remove '°' and all next characters, you can :

    string txt = "+0°1,0'";
    string degree = txt.Remove(txt.IndexOf('°'));
    // degree => "+0"