Home > DeveloperSection > Forums > Replace characters in between string
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    March-29-2014 3:27 AM

 C# C# 
Ratings:


 1 Reply(s)
 671  View(s)
Rate this:

I have the following string:

string input = "  2  1";

It is 2 spaces followed by a 2 and then 2 more spaces followed by a 1. I want to do a replace on it, but I only want to replace the spaces between the 2 and the 1 with 0's and I do not want to use RegEx if possible?

The end result should look like this:

string result = "  2001";



Pravesh Singh

Total Post:411

Points:2881
Posted on    March-29-2014 7:01 AM

Hi Marcel,
Try this,

static string ReplaceSpacesWithZerosExceptLeading(string s)

{

    return s.TrimStart(' ').Replace(' ', '0').PadLeft(s.Length);

}


Don't want to miss updates? Please click the below button!

Follow MindStick