Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing few different characters in a string

I try to replace few characters in some string 14/04/2010 17:12:11 and get, for example, next result:

14%04%2010%17%12%11

I know about method Replace, but its definition looks like Replace(Char,Char). Which means using it 3 times in method chain. Doesn't look idiomatic. How to solve the problem in an optimal way? Regular expressions? Any ways to escape them?

like image 759
akrisanov Avatar asked Dec 06 '22 02:12

akrisanov


2 Answers

Chain it:

string s1 = "14/04/2010 17:12:1";

string s2 = s1.Replace("/","%").Replace(" ","%").Replace(":","%");
like image 176
Paul Sasik Avatar answered Dec 08 '22 01:12

Paul Sasik


Regex.Replace(myString, "[/ :]", "%");

Simple, yet elegant!

like image 36
Aren Avatar answered Dec 08 '22 01:12

Aren