string read only error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    string read only error

    Hi,
    I wanted to replace all ',' with ';' but compiler gives
    me an error that my subLine is read only
    foreach ( ListViewItem.Li stViewSubItem subItem in
    Item.SubItems)
    {
    subLine = subItem.Text;
    int nIndex;
    while ( (nIndex = subLine.LastInd exOf(',' ) ) != -
    1 )
    subLine[nIndex] = ';';
    }
    Thanks
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: string read only error

    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.Li stViewSubItem subItem in Item.SubItems)
    subItem.Text = subItem.Text.Re place(',', ';');

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m


    "David" <anonymous@disc ussions.microso ft.com> wrote in message
    news:0c6101c399 75$94c24990$a30 1280a@phx.gbl.. .[color=blue]
    > Hi,
    > I wanted to replace all ',' with ';' but compiler gives
    > me an error that my subLine is read only
    > foreach ( ListViewItem.Li stViewSubItem subItem in
    > Item.SubItems)
    > {
    > subLine = subItem.Text;
    > int nIndex;
    > while ( (nIndex = subLine.LastInd exOf(',' ) ) != -
    > 1 )
    > subLine[nIndex] = ';';
    > }
    > Thanks[/color]


    Comment

    Working...