0

Anyone knows why it tells me can't edit chaine it's in read only ?!

public static string FormaterChainePascalOuMixte(String chaine)
    {
        for (int i = 0; i < chaine.Length; i++)
        {
            char xxx = char.ToUpper(chaine[i]);
            if (i != 0 && chaine[i] == xxx)
            {
                chaine[0] = char.ToLower(xxx);
            }
        }
    }
user2180198
  • 5
  • 2
  • 3

1 Answers1

0

Because strings are immutable, it is not possible (without using unsafe code) to modify the value of a string object after it has been created. However, there are many ways to modify the value of a string and store the result in a new string object. Reference: here

You could change your code as follows:

public static string FormaterChainePascalOuMixte(String chaine)
    {
        if (String.IsNullOrEmpty(chaine))
            return null;

        char[] charArray = chaine.ToCharArray();

        for (int i = 0; i < charArray.Length; i++)
        {
            char xxx = char.ToUpper(charArray[i]);
            if (i != 0 && charArray[i] == xxx)
            {
                charArray[0] = char.ToLower(xxx);
            }
        }

        return charArray.ToString();
    }
cristis
  • 56
  • 4