[英]String manipulation with C# .NET
I need help with string manipulation in c#. 我需要c#中字符串操作的帮助。
I have string with words and empty spaces between them(I have more than one empty spaces between words, empty spaces are dynamic). 我有字符串和空格之间的空格(我在单词之间有多个空格,空格是动态的)。 I need to replace empty spaces with dash "-"
我需要用短划线“ - ”替换空格
I have something like this: 我有这样的事情:
string stringForManipulation = "word1 word2 word3";
I need to have this: 我需要这个:
"word1-word2-word3"
Tnx TNX
var result = Regex.Replace(stringForManipulation , @"\s+", "-");
s
表示空格, +
表示一个或多个出现。
You can use regular expressions: 您可以使用正则表达式:
string stringForManipulation = "word1 word2 word3";
string result = Regex.Replace(stringForManipulation, @"\s+", "-");
This will replace all occurences of one or more whitespace to "-". 这将把一个或多个空格的所有出现替换为“ - ”。
对于不知道正则表达式的thoose,可以通过简单的拆分连接操作来实现:
string wordsWithDashes = stringForManipulation.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries).Join('-')
Simply use 简单地使用
string result=Regex.Replace(str , @"\s+", "-")
Will replace single or multiple spaces with single '-' 用单个' - '替换单个或多个空格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.