[英]How to remove the last comma and space from string (if there) using regex?
我在C#应用程序中有一个字符串,并想知道检查最后两个字符(逗号和空格)的正确方法是什么,
并使用正则表达式删除它们(如果是这样)。
You don't need Regex for that, use String.TrimEnd
like: 你不需要Regex,使用
String.TrimEnd
如:
string updatedString = yourString.TrimEnd(',', ' ');
You can also specify a string and call ToCharArray
like: 您还可以指定一个字符串并调用
ToCharArray
如:
string str = "something, , ,,,, ";
string updatedString = str.TrimEnd(", ".ToCharArray());
would give you "something"
会给你
"something"
If you only want to remove a single occurrence of ", "
(comma and space at the end) then use: 如果您只想删除单个
", "
(最后的逗号和空格),请使用:
if (str.EndsWith(", "))
updatedString = str.Substring(0, str.Length - 2);
Rather than removing the trailing comma and space it's easier to simply generate the string without an extra comma in the first place. 不是删除尾随的逗号和空格,而是首先简单地生成字符串而不需要额外的逗号。 When generating the comma separated values simply use
String.Join
to join all of the strings together with a particular separator: 生成逗号分隔值时,只需使用
String.Join
将所有字符串与特定分隔符连接在一起:
var line = string.Join(", ", yourCollectionOfValues);
It'll be easier and faster than appending the values together, and Join
will already handle ensure that there is no trailing separator. 它比将值附加在一起更容易,更快,
Join
已经处理,确保没有尾随分隔符。
(Sorry I was late the the party.) (对不起,我迟到了。)
Yes , this is not something for which you have to or should use a regex; 是的 ,这不是你必须或应该使用正则表达式的东西; but since you asked how to do it with a regex (eg maybe you are just curious, and "suppose I have to to this with a regex" hypotheticals are a good way to learn), consider the following pattern:
但是既然你问过如何使用正则表达式(例如,也许你只是好奇,并且“假设我必须使用正则表达式”假设是一种很好的学习方法),请考虑以下模式:
(.*?)(, )?$
You can test it in a related regex fiddle . 您可以在相关的正则表达式中进行测试。
Key points: 关键点:
(.*?)
– Match zero or more ( *
) of any character except newline ( .
) as few times as possible ( ?
). (.*?)
- 尽可能少的几次 ( ?
)匹配除换行符( .
)之外的任何字符的零或多( *
)。 (, )?$
– Match zero or one ( ?
) ,
at the end ( $
). (, )?$
- 匹配零或一( ?
) ,
结尾 ( $
)。 Also, the following C# example with this pattern... 此外,以下C#示例使用此模式...
var str1 = "whatever, ";
var str2 = "whatever, ,";
var str3 = "";
var regex = new Regex("(.*?)(, )?$");
var str1Match = regex.Match(str1);
var str2Match = regex.Match(str2);
var str3Match = regex.Match(str3);
Console.WriteLine(str1Match.Groups[1].Value);
Console.WriteLine(str2Match.Groups[1].Value);
Console.WriteLine(str3Match.Groups[1].Value);
...produces the following outputs: ...产生以下输出:
str1
( "whatever, "
) => whatever
str1
( "whatever, "
) => whatever
str2
( "whatever, ,"
) => whatever, ,
str2
( "whatever, ,"
) => whatever, ,
str3
( ""
) => str3
( ""
) => It uses Groups[1].Value
to get the first capture group's value (ie the value matched by (.*?)
rather than (, )?
(if any)). 它使用
Groups[1].Value
来获取第一个捕获组的值(即匹配的值(.*?)
而不是(, )?
(如果有的话))。
Edit: 编辑:
I actually like what @UlugbekUmirov suggested in his comment ( string output = Regex.Replace("my string, ", ", $", "");
) even better because it is super simple; 我实际上喜欢@UlugbekUmirov在他的评论中提出的建议(
string output = Regex.Replace("my string, ", ", $", "");
)甚至更好,因为它非常简单; but hopefully you find the approach I outlined instructive. 但希望你发现我概述的方法很有启发性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.