---
title: "Replace characters in between string"  
description: "Replace characters in between string"  
author: "marcel ethan"  
published: 2014-03-29  
updated: 2014-03-29  
canonical: https://www.mindstick.com/forum/1996/replace-characters-in-between-string  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Replace characters in between string

I have the following string:

string [input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java) = " 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](https://yourviews.mindstick.com/view/81330/cow-antibody-research-in-usa-a-step-ahead-to-replace-plasma-therapy) 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](https://www.mindstick.com/forum/160352/how-to-use-regex-in-javascript-for-pattern-matching) if possible?

The [end](https://yourviews.mindstick.com/view/88503/us-iran-war-live-updates-trump-says-us-in-no-rush-to-end-iran-war) result should look like this:

[string result](https://www.mindstick.com/forum/12841/in-mvc-how-do-i-return-a-string-result) = " 2001";

## Replies

### Reply by Pravesh Singh

Hi Marcel,Try this,\

```
static string ReplaceSpacesWithZerosExceptLeading(string s){    return
s.TrimStart(' ').Replace(' ', '0').PadLeft(s.Length);}
```


---

Original Source: https://www.mindstick.com/forum/1996/replace-characters-in-between-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
