Saturday, March 31, 2012

Why is this a read-only?

I'm trying to loop thru a string and change any character that is not a letter or number to an underscore (so a string of 'Hello%hH' would end up as 'Hello_hH')

Here is the code, but it telling me that the 'strNameArtist1 = '_' line is a read-only, and I cannot change the current index in the string.. so what do I do, how can I make this work? Am I even going about this the right way?


string strNameArtist1 = Server.UrlDecode(Request.QueryString["nameartist"].Replace("'", "''"));

for (int i = 0; i < strNameArtist1.Length; ++i)
{
if (char.IsLetterOrDigit(strNameArtist1[i]) == false)
strNameArtist1[i] = '_'; // complile error says cannot assign to, it is read only... ??
}

You can't access parts of a string using an index. Try the Replace method.

0 comments:

Post a Comment