Replicate one or more spaces in%

I have a line like "First Last Name" I want to replace empty spaces with%, for example

 "First%Last%Name" 

How to replace consecutive spaces with one%?

+4
source share
2 answers

You can do this with a regex:

 str = Regex.Replace(str, " +", "%"); 
+7
source
 var result = string.Join("%", str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); 
+1
source

Source: https://habr.com/ru/post/1333892/


All Articles