0
public string simplifyString(string sInput)
{
        if (sInput.Length < 2)
        {
            return sInput;
        } 

        string sOutput;
        int iCount = 0;

        for (int i=1; i < sInput.Length; i++)
        {
            if (sInput[i] != sInput[iCount])
            {
                iCount++;
                sOutput[iCount] = sInput[i];
            }
        }
        return sOutput;
}

The precompiler has a problem with the above C# code.

sOutput[iCount] = sInput[i];

this line has an error. It says that string.this[int] cannot be assigned and is read only.

3
  • What are you exactly trying to achieve? Commented Oct 12, 2015 at 8:20
  • He's right, strings are immutable. You need to create a new string, for example with the StringBuilder. Commented Oct 12, 2015 at 8:20
  • Yes, sOutput[iCount] returns the character of that index and since strings are immutable, that's normal to be a read only. Commented Oct 12, 2015 at 8:21

3 Answers 3

2

A string in .NET is immutable, once created it cannot be changed.

If you only need to replace characters (not remove or add) then you can simply convert it to an array before and back to a string afterwards:

var a = sOutput.ToCharArray();
// code that modifies a
var s = new string(a);

If you need to be able to remove or add as well, you can use a StringBuilder:

var sb = new StringBuilder(sOutput);
// code that modifies sb
var s = sb.ToString();
Sign up to request clarification or add additional context in comments.

1 Comment

Even if you just have to replace characters i'd prefer StringBuilder. You can use similar code: var sb = new StringBuilder(sOutput);sb[iCount] = sInput[i]; but you can still use it if you want to add or remove characters later.
0

If you change your code to like below, I think your problem will be solved:

public string simplifyString(string sInput)
{
    if (sInput.Length < 2)
    {
        return sInput;
    } 

    string sOutput = sInput[0].ToString(); //Add initial for string
    int iCount = 0;

    for (int i=1; i < sInput.Length; i++)
    {
        if (sInput[i] != sInput[iCount])
        {
            iCount++;
            sOutput += sInput[i].ToString();//add new char as string to the end of the string
        }
    }
    return sOutput;
}

3 Comments

This works but passing strings to this method is having some problems.
string sOne = "abcdefg"; string out1 = simplifyString(ref sOne); // argument should be passed without ref keyword
you get one string, do somethings with it and make another one, then return the second one. Why do you need the first one to pass as ref? you did not change it even.
0

This is an area where budding c# programmers err! string is immutable in C#,i.e., you can't modify a string in C# !

Each time you want to edit a portion of the string you need to create a new string object and provide it with the edited values. however if your program has too many of such operations it would be it would have a appreciable memory overhead ! all this memory won't be freed until the Garbage Collector of .net decides to run! If your string involves lot of editing operations use string builder!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.