Strange TrimEnd behaviour with \ char

前端 未结 2 1234
有刺的猬
有刺的猬 2020-12-21 22:11

I am using TrimEnd to remove certain characters from the end of a string. Initially I thought this would work:

    Dim strNew As String = \"Employees\\Sickne         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 22:45

    Re-read the documentation of TrimEnd; you are using it wrong: TrimEnd will remove any char that is in the array from the end of the string, as long as it still finds such chars.

    For example:

    Dim str = "aaebbabab"
    Console.WriteLine(str.TrimEnd(new Char() { "a"c, "b"c })
    

    will output aa since it removes all trailing as and bs.

    If your input looks exactly like in your example, your easiest recurse is to use Substring:

    Console.WriteLine(strNew.Substring(0, strNew.Length - strTrim.Length))
    

    Otherwise you can resort to regular expressions.

提交回复
热议问题