【发布时间】:2019-11-12 16:06:00
【问题描述】:
我将一个字符串作为输入,并且由于稍后进行一些处理,因此该字符串在字符串中的任何位置不包含 2 个或更多连续空格是至关重要的。
例如
string foo = "I am OK" is a valid string
string foo = " I am OK " is a valid string
string foo = " I am OK" is NOT a valid string due to the initial double white space
string foo = "I am OK " is NOT a valid string due to the trailing double whitespaces
string foo = " I am OK " is NOT a valid string since it has 2 whitespaces between am and OK
我想你明白了,我尝试使用以下代码规范化字符串
string normalizedQuery = apiInputObject.Query.Replace(" ", "");
但这仅适用于我确定字符串中有单个空格,这就是为什么我需要确保字符串永远不会有更多这样我可以使用该替换。
如何确保字符串符合我的格式?
【问题讨论】:
标签: c# string whitespace removing-whitespace