How to change 1 char in the string?

后端 未结 7 1792
天涯浪人
天涯浪人 2021-01-03 17:43

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:



        
相关标签:
7条回答
  • 2021-01-03 17:49

    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().

    0 讨论(0)
  • 2021-01-03 17:52

    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);
    
    0 讨论(0)
  • 2021-01-03 17:54

    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';
    
    0 讨论(0)
  • 2021-01-03 18:02
    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?

    0 讨论(0)
  • 2021-01-03 18:11

    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.

    0 讨论(0)
  • 2021-01-03 18:14

    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();
    }
    
    0 讨论(0)
提交回复
热议问题