coure2011
coure2011

Reputation: 42464

Replacint single or multiple space to %

I have string like "First Last Name" I want to replace empty spaces to % like

"First%Last%Name"

How to replace consecutive spaces to single %?

Upvotes: 4

Views: 109

Answers (2)

dtb
dtb

Reputation: 217341

var result = string.Join("%",
    str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838696

You can do it with a regular expression:

str = Regex.Replace(str, " +", "%");

Upvotes: 6

Related Questions