问题
I am developing web app using C#. I want to replace multiple spaces with single space in between string. I tried with normal string replace function, but it was not helpful. It is possible with Regular Expression, but I don't have clear idea about that. Please provide a example code for the following string:
Actual String:
Have a Nice Day ! !!
Needed:
Have a Nice Day !!!
回答1:
You can match the following:
@"\s+"
and replace with:
" "
Regex.Replace("Have a Nice Day ! !!", @"\s+", " ");
回答2:
See if there are two or more spaces exist, if so replace it with single space.
var subject = "Have a Nice Day ! !!";
var result = Regex.Replace(subject,@"\s{2,}"," ");
来源:https://stackoverflow.com/questions/23800371/how-to-replace-multiple-spaces-with-single-space