Reputation: 402
public struct Osoba{
public string nazwisko;
[...]
}
for (int k = 0; k < l; k++)
{
dlugosc = osoba[k].nazwisko.Length;
[...]
if (c[0] == 's' && c[1] == 'k' && c[2] == 'i')
{
osoba[k].nazwisko[dlugosc - 3] = '*';
osoba[k].nazwisko[dlugosc - 2] = '*';
osoba[k].nazwisko[dlugosc - 1] = '*';
}
}
Hello there, I keep trying to replace 3 last letters of string, but i get this error:
Property or indexer 'string.this[int]' cannot be assigned to -- it is read only\
I tried to google it, the solution was mostly adding getters and setters (i've yet to learn about it) but it didn't help me. Why can't I modify my string even thought i set everything as public?
Upvotes: 2
Views: 8781
Reputation: 1038710
Strings are immutable in .NET. It's quite unclear what you are trying to achieve but if you want to directly manipulate the contents you could use a char array instead:
public struct Osoba
{
public char[] nazwisko;
[...]
}
Upvotes: 2
Reputation: 101681
Strings
are immutable.You can't change a string after you created it.You need to create a new string and assign it back:
osoba[k].nazwisko = osoba[k].nazwisko.Substring(0, dlugosc - 3) + "***";
Upvotes: 5