1

If i have a string containing three 0 values, how would i grab them one by one in order to replace them?

the 0's could be located anywhere in the string.

i don't want to use regex.

example string to parse:

String myString = "hello 0 goodbye 0 clowns are cool 0"; 

right now i can only find the three 0 values if they are right next to each other. i replace them using stringToParse.Replace("0", "whatever value i want to replace it with");

I want to be able to replace each instance of 0 with a different value...

sll
  • 61,540
  • 22
  • 104
  • 156
BigBug
  • 6,202
  • 23
  • 87
  • 138
  • 3
    I must be missing something, as `string.Replace` which you are using is the right answer. – Oded Feb 04 '12 at 09:22
  • 2
    Your current code already replaces all of them. I don't understand your problem. Do you want to replace them with different values? – CodesInChaos Feb 04 '12 at 09:22
  • 2
    He wants to replace every occurance with a different value i guess – juergen d Feb 04 '12 at 09:23
  • @juergend Yes, you're right, i want to replace every instance with a different value.. sorry, i should have specified – BigBug Feb 04 '12 at 09:26
  • 2
    Here is a solution: http://stackoverflow.com/questions/721299/net-string-replace – juergen d Feb 04 '12 at 09:28
  • Here is an even better solution: http://stackoverflow.com/a/141076/575376 – juergen d Feb 04 '12 at 09:35
  • possible duplicate of [How do I replace the *first instance* of a string in .NET?](http://stackoverflow.com/questions/141045/how-do-i-replace-the-first-instance-of-a-string-in-net) – David Waters Feb 04 '12 at 09:42

5 Answers5

4

You can do something like this:

var strings = myString.Split('0');
var replaced = new StringBuilder(strings[0]);

for (var i = 1; i < strings.Length; ++i)
{
    replaced.Append("REPLACED " + i.ToString());
    replaced.Append(strings[i]);
}
fardjad
  • 20,031
  • 6
  • 53
  • 68
2

pseudolang :

s = "yes 0 ok 0 and 0"
arr = s.split(" 0")
newstring = arr[0] + replace1  + arr[1] + replace2 + arr[2] + replace3
Peter
  • 47,963
  • 46
  • 132
  • 181
1

If you have control of these input strings, then I would use a composite format string instead:

string myString = "hello {0} goodbye {1} clowns are cool {2}";
string replaced = string.Format(myString, "replace0", "replace1", "replace2");
devdigital
  • 34,151
  • 9
  • 98
  • 120
1

Using LINQ and generic function to decouple replacement logic.

var replace = (index) => {
                    // put any custom logic here
                    return (char) index;
                 };

string input = "hello 0 goodbye 0 clowns are cool 0";       
string output = new string(input.Select((c, i) => c == '0' ? replace(i) : c)
                                .ToArray());

Pros:

  • Char replacement logic decoupled from the string processing (actually LINQ query)

Cons:

  • Not the best solution from performance perspectives
BenMorel
  • 34,448
  • 50
  • 182
  • 322
sll
  • 61,540
  • 22
  • 104
  • 156
1
public string ReplaceOne(string full, string match, string replace)
{
    int firstMatch = full.indexOf(match);
    if(firstMatch < 0)
    {
        return full;
    }
    string left;
    string right;
    if(firstMatch == 0)
        left = "";
    else
        left = full.substring(0,firstMatch);
    if(firstMatch + match.length >= full.length)
        right = "";
    else
        right = full.substring(firstMatch+match.length);
    return left + replace + right 
}

If your match can occur in replace, then you will want to track what index your upto and pass it in to indexOf.

David Waters
  • 11,979
  • 7
  • 41
  • 76