string read only error

  • Thread starter Thread starter David
  • Start date Start date
D

David

Hi,
I wanted to replace all ',' with ';' but compiler gives
me an error that my subLine is read only
foreach ( ListViewItem.ListViewSubItem subItem in
Item.SubItems)
{
subLine = subItem.Text;
int nIndex;
while ( (nIndex = subLine.LastIndexOf(',' ) ) != -
1 )
subLine[nIndex] = ';';
}
Thanks
 
David,

Strings are immutable in .NET. Because of this, you will have to create
a new string, replacing the character in the string with the character that
you want. Basically, you should use the Replace method on the string, which
will return a new string with the replaced characters, like so:

foreach ( ListViewItem.ListViewSubItem subItem in Item.SubItems)
subItem.Text = subItem.Text.Replace(',', ';');

Hope this helps.
 
Back
Top