17

I didn't get the problem - I was trying to do a simple action:

for(i = x.Length-1, j = 0 ; i >= 0 ; i--, j++)
{
    backx[j] = x[i];
}

Both are declared:

String x;
String backx;

What is the problem ? It says the error in the title... If there is a problem - is there another way to do that? The result (As the name 'backx' hints) is that backx will contain the string X backwards. P.S. x is not empty - it contains a substring from another string.

user3220493
  • 267
  • 2
  • 4
  • 8
  • 1
    possible duplicate of [Edit characters in a String in C#](http://stackoverflow.com/questions/2931288/edit-characters-in-a-string-in-c-sharp) – quetzalcoatl Feb 28 '14 at 11:35

9 Answers9

25

Strings are immutable: you can retrieve the character at a certain position, but you cannot change the character to a new one directly.

Instead you'll have to build a new string with the change. There are several ways to do this, but StringBuilder does the job in a similar fashion to what you already have:

StringBuilder sb = new StringBuilder(backx);
sb[j] = x[i];
backx = sb.ToString();

EDIT: If you take a look at the string public facing API, you'll see this indexer:

public char this[int index] { get; }

This shows that you can "get" a value, but because no "set" is available, you cannot assign values to that indexer.

EDITx2: If you're looking for a way to reverse a string, there are a few different ways, but here's one example with an explanation as to how it works: http://www.dotnetperls.com/reverse-string

Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
3

String is immutable in .NET - this is why you get the error.

You can get a reverse string with LINQ:

string x = "abcd";
string backx = new string(x.Reverse().ToArray()); 

Console.WriteLine(backx);    // output: "dcba"
w.b
  • 11,026
  • 5
  • 30
  • 49
2

String are immuatable. You have convert to Char Array and then you would be able to modify. Or you can use StringBuilder.

for example

char[] wordArray = word.ToCharArray();
1

In C# strings are immutable. You cannot "set" Xth character to whatever you want. If yo uwant to construct a new string, or be able to "edit" a string, use i.e. StringBuilder class.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
1

Strings are immutable in C#. You can read more about it here: http://msdn.microsoft.com/en-us/library/362314fe.aspx

Janis Kirsteins
  • 2,128
  • 16
  • 17
0

Both the variables you have are string while you are treating them as if they were arrays (well, they are). Of course it is a valid statement to access characters from a string through this mechanism, you cannot really assign it that way.

Since you are trying to reverse a string, do take a look at this post. It has lot of information.

Community
  • 1
  • 1
danish
  • 5,550
  • 2
  • 25
  • 28
0
    public static string ReverseName( string theName)
    {
        string revName = string.Empty;
        foreach (char a in theName)
        {
            revName = a + revName;
        }

        return revName;
    }

This is simple and does not involve arrays directly.

M Wat
  • 39
  • 4
0

The code below simply swaps the index of each char in the string which enables you to only have to iterate half way through the original string which is pretty efficient if you're dealing with a lot of characters. The result is the original string reversed. I tested this with a string consisting of 100 characters and it executed in 0.0000021 seconds.

private string ReverseString(string testString)
    {

        int j = testString.Length - 1;
        char[] charArray = new char[testString.Length];

        for (int i = 0; i <= j; i++)
        {

            if (i != j)
            {
                charArray[i] = testString[j];
                charArray[j] = testString[i];
            }

            j--;
        }

        return new string(charArray);
    }
netexile
  • 16
  • 1
0

In case you need to replace e.g. index 2 in string use this (it is ugly, but working and is easily maintainbable)

V1 - you know what you want to put their. Here you saying in pseudocode string[2] = 'R';

row3String.Replace(row3String[2], 'R');

V2 - you need to put their char R or char Y. Here string[2] = 'R' if was 'Y' or if was not stay 'Y' (this one line if needs some form of else)

row3String.Replace(row3String[2], row3String[2].Equals('Y') ? 'R' : 'Y');
Jan Lacina
  • 31
  • 7