C# Trim string regardless of characters

后端 未结 4 594
无人及你
无人及你 2021-01-24 21:47

So here the situation.

I have multiple strings that begin and end with a random amount of spaces. The problem is the string contains multiple words so I can\'t just rep

4条回答
  •  一生所求
    2021-01-24 22:15

    Assuming the quotes are really in there, then you want to use a regular expression:

    (["'])\s*(.*[^\s])\s*(["'])
    

    Simply replace it with:

    $1$2$3
    

    So:

    string value = Regex.Replace("\"   value to trim   \"", @"([""'])\s*(.*[^\s])\s*([""'])", "$1$2$3");
    

提交回复
热议问题