72

I have this code:

string str = "valta is the best place in the World";

I need to replace the first symbol. When I try this:

str[0] = 'M';

I received an error. How can I do this?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Tadeusz
  • 6,453
  • 9
  • 40
  • 58
  • 1
    Whatever error he is getting is a good one. Strings are immutable objects, it's normal for the compiler to complain about that instruction... – Radu Murzea Jan 24 '12 at 13:00
  • @Oded - there are a number of reasons why this won't work. String is immutable in .NET, unlike, for example, Delphi, where this would work. You can't change string characters by index like this. C# also has no implicit conversions from strings to char - 'M' would have to be declared as {char c = 'M'}. – J... Jan 24 '12 at 13:02
  • 2
    @J... - True enough. My comment was more on the "how to ask a question" side of things. – Oded Jan 24 '12 at 13:10
  • Possible duplicate of [Replacing a char at a given index in string?](http://stackoverflow.com/questions/9367119/replacing-a-char-at-a-given-index-in-string) – RedX Feb 17 '16 at 17:15

12 Answers12

72

Strings are immutable, meaning you can't change a character. Instead, you create new strings.

What you are asking can be done several ways. The most appropriate solution will vary depending on the nature of the changes you are making to the original string. Are you changing only one character? Do you need to insert/delete/append?

Here are a couple ways to create a new string from an existing string, but having a different first character:

str = 'M' + str.Remove(0, 1);

str = 'M' + str.Substring(1);

Above, the new string is assigned to the original variable, str.

I'd like to add that the answers from others demonstrating StringBuilder are also very appropriate. I wouldn't instantiate a StringBuilder to change one character, but if many changes are needed StringBuilder is a better solution than my examples which create a temporary new string in the process. StringBuilder provides a mutable object that allows many changes and/or append operations. Once you are done making changes, an immutable string is created from the StringBuilder with the .ToString() method. You can continue to make changes on the StringBuilder object and create more new strings, as needed, using .ToString().

Kevin P. Rice
  • 5,550
  • 4
  • 33
  • 39
  • Make sure to prevent null exceptions by adding this line `if (!string.IsNullOrEmpty(str))` right before your preferred method. – Baz Guvenkaya Aug 16 '18 at 03:25
  • Alternatively. You can do `str[1..]` (Range from index 1 to `Length` of `str`) to skip the 1st character in `str`. I personally find ranges cleaner to read. – Galacticai Nov 02 '21 at 18:12
53

I suggest you to use StringBuilder class for it and than parse it to string if you need.

System.Text.StringBuilder strBuilder = new System.Text.StringBuilder("valta is the best place in the World");
strBuilder[0] = 'M';
string str=strBuilder.ToString();

You can't change string's characters in this way, because in C# string isn't dynamic and is immutable and it's chars are readonly. For make sure in it try to use methods of string, for example, if you do str.ToLower() it makes new string and your previous string doesn't change.

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
28

Strings are immutable. You can use the string builder class to help!:

string str = "valta is the best place in the World";

StringBuilder strB = new StringBuilder(str);

strB[0] = 'M';
Morgoth
  • 4,935
  • 8
  • 40
  • 66
Jeb
  • 3,689
  • 5
  • 28
  • 45
  • @Bobbity Bob can you be a little more specific why this is incorrect? All I think is that I haven't assigned back to a string from the string builder (by using its ToString method) – Jeb Sep 18 '13 at 13:23
  • I have no idea what I meant by that. i just ran the code and it works. if you edit your answer (like insert a space somewhere) I will upvote it too.. seems fine now... – Boppity Bop Sep 18 '13 at 14:05
16

While it does not answer the OP's question precisely, depending on what you're doing it might be a good solution. Below is going to solve my problem.

Let's say that you have to do a lot of individual manipulation of various characters in a string. Instead of using a string the whole time use a char[] array while you're doing the manipulation. Because you can do this:

 char[] array = "valta is the best place in the World".ToCharArray();

Then manipulate to your hearts content as much as you need...

 array[0] = "M";

Then convert it to a string once you're done and need to use it as a string:

string str = new string(array);
badweasel
  • 2,349
  • 1
  • 19
  • 31
  • 1
    But isn't `StringBuilder` a better choice than a `char[]`? `StringBuilder` has methods for append, insert, remove, and format, while a `char[]` is going to pretty much stay fixed length. I suppose if I were dealing with a fixed length string with only single character manipulations I might choose `char[]`. – Kevin P. Rice Aug 01 '15 at 22:51
  • 2
    My case was for a fixed length string. So in my case I really just want the fastest way to replace a character. I wasn't knocking other methods. Just sharing a valid method that worked for me. – badweasel Aug 02 '15 at 02:44
  • 1
    Exactly what I needed just now, so thanks for sharing anyway. – Whelkaholism Apr 29 '16 at 10:46
  • Good point, `char[]` is much simpler and more intuitive than `StringBuilder` for the task in question. – Anton Shepelev Jul 06 '18 at 19:45
10

Merged Chuck Norris's answer w/ Paulo Mendonça's using extensions methods:

/// <summary>
/// Replace a string char at index with another char
/// </summary>
/// <param name="text">string to be replaced</param>
/// <param name="index">position of the char to be replaced</param>
/// <param name="c">replacement char</param>
public static string ReplaceAtIndex(this string text, int index, char c)
{
    var stringBuilder = new StringBuilder(text);
    stringBuilder[index] = c;
    return stringBuilder.ToString();
}
natenho
  • 5,231
  • 4
  • 27
  • 52
10

I made a Method to do this

    string test = "Paul";
    test = ReplaceAtIndex(0, 'M', test);

    // (...)

    static string ReplaceAtIndex(int i, char value, string word)
    {
        char[] letters = word.ToCharArray();
        letters[i] = value;
        return string.Join("", letters);
    }
Paulo Mendonça
  • 635
  • 12
  • 28
6
str = "M" + str.Substring(1);

If you'll do several such changes use a StringBuilder or a char[].

(The threshold of when StringBuilder becomes quicker is after about 5 concatenations or substrings, but note that grouped concatenations of a + "b" + c + d + "e" + f are done in a single call and compile-type concatenations of "a" + "b" + "c" don't require a call at all).

It may seem that having to do this is horribly inefficient, but the fact that strings can't be changes allows for lots of efficiency gains and other advantages such as mentioned at Why .NET String is immutable?

Community
  • 1
  • 1
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
4

I found a solution in unsafe context:

    string str = "gg"; char c = 'H'; int index = 1;
    fixed (char* arr = str) arr[index] = 'H';
    Console.WriteLine(str);

It's so simple. And in safe context:

    string str = "gg"; char c = 'H'; int index = 1;
    GCHandle handle = GCHandle.Alloc(str, GCHandleType.Pinned);
    IntPtr arrAddress = handle.AddrOfPinnedObject();
    Marshal.WriteInt16(arrAddress + index * sizeof(char), c);
    handle.Free();
    Console.WriteLine(str);
Tadeusz
  • 6,453
  • 9
  • 40
  • 58
  • It's quite weird to broke the fundamental string immutable functionality, as a default C# dev won't expect such a turnaround. But TBH I came here for this kind of answer (still not gonna use it in prod) – Serhii Aug 23 '21 at 11:03
4

If speed is important, then you can do this:

String strValue = "$Some String Here!";
strValue = strValue.Remove(0, 1).Insert(0, "*");

This way, you don't really reconstruct the string, you keep the original object and just removing the first character and inserting a new one.

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
1

Can also be done using C# 8 ranges:

str = "M" + str[1..];

or + string interpolation:

str = $"M{str[1..]}";
Ed'ka
  • 6,595
  • 29
  • 30
0

I usually approach it like this:

   char[] c = text.ToCharArray();
   for (i=0; i<c.Length; i++)
   {
    if (c[i]>'9' || c[i]<'0') // use any rules of your choice
    {
     c[i]=' '; // put in any character you like
    }
   }
   // the new string can have the same name, or a new variable       
   String text=new string(c); 
user3029478
  • 179
  • 1
  • 2
0

Whilst probably not the best use of LINQ, here's a solution using LINQ:

string str = "valta is the best place in the world";
int index = 0;
char ch = 'M';
str = string.Join("", str.Select((c, i) => i == index ? ch : c));

If there are subsequent LINQ operations you could chain them together:

string str = "valta is the best place in the world";
str = string.Join("", str.Select((c, i) => i == 0 ? 'P' : c)
                         .Select((c, i) => i == 1 ? 'a' : c)
                         .Select((c, i) => i == 2 ? 'r' : c)
                         .Select((c, i) => i == 3 ? 'i' : c)
                         .Select((c, i) => i == 4 ? 's' : c));
Stephen Quan
  • 21,481
  • 4
  • 88
  • 75