Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a char at a given index in string? [duplicate]

Tags:

string

c#

String does not have ReplaceAt(), and I'm tumbling a bit on how to make a decent function that does what I need. I suppose the CPU cost is high, but the string sizes are small so it's all ok

like image 860
Jason94 Avatar asked Feb 20 '12 19:02

Jason94


People also ask

How do you replace a character in a string in Java?

String are immutable in Java. You can't change them. You need to create a new string with the character replaced.


5 Answers

Use a StringBuilder:

StringBuilder sb = new StringBuilder(theString);
sb[index] = newChar;
theString = sb.ToString();
like image 53
Thomas Levesque Avatar answered Oct 05 '22 21:10

Thomas Levesque


The simplest approach would be something like:

public static string ReplaceAt(this string input, int index, char newChar)
{
    if (input == null)
    {
        throw new ArgumentNullException("input");
    }
    char[] chars = input.ToCharArray();
    chars[index] = newChar;
    return new string(chars);
}

This is now an extension method so you can use:

var foo = "hello".ReplaceAt(2, 'x');
Console.WriteLine(foo); // hexlo

It would be nice to think of some way that only required a single copy of the data to be made rather than the two here, but I'm not sure of any way of doing that. It's possible that this would do it:

public static string ReplaceAt(this string input, int index, char newChar)
{
    if (input == null)
    {
        throw new ArgumentNullException("input");
    }
    StringBuilder builder = new StringBuilder(input);
    builder[index] = newChar;
    return builder.ToString();
}

... I suspect it entirely depends on which version of the framework you're using.

like image 32
Jon Skeet Avatar answered Oct 05 '22 21:10

Jon Skeet


string s = "ihj";
char[] array = s.ToCharArray();
array[1] = 'p';
s = new string(array);
like image 25
Maciej Avatar answered Oct 05 '22 21:10

Maciej


Strings are immutable objects, so you can't replace a given character in the string. What you can do is you can create a new string with the given character replaced.

But if you are to create a new string, why not use a StringBuilder:

string s = "abc";
StringBuilder sb = new StringBuilder(s);
sb[1] = 'x';
string newS = sb.ToString();

//newS = "axc";
like image 40
Petar Ivanov Avatar answered Oct 05 '22 22:10

Petar Ivanov


I suddenly needed to do this task and found this topic. So, this is my linq-style variant:

public static class Extensions
{
    public static string ReplaceAt(this string value, int index, char newchar)
    {
        if (value.Length <= index)
            return value;
        else
            return string.Concat(value.Select((c, i) => i == index ? newchar : c));
    }
}

and then, for example:

string instr = "Replace$dollar";
string outstr = instr.ReplaceAt(7, ' ');

In the end I needed to utilize .Net Framework 2, so I use a StringBuilder class variant though.

like image 34
horgh Avatar answered Oct 05 '22 21:10

horgh