Closed Thread
Results 1 to 4 of 4

Thread: Accessing a string by array

  1. #1
    Saint is offline Learning Programmer
    Join Date
    Aug 2006
    Posts
    63
    Rep Power
    0

    Accessing a string by array

    I get an error when using this code:

    Code:
    string str = "ahe string";
    str[0]='T';
    it says the indexer is read only. How can I accomplish the same thing?
    Hi >> Saint

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101
    Shouldnt you declare the array like pascal or so? I dont know coz i dont C# but just a curiosity!

  4. #3
    brackett is offline Programmer
    Join Date
    May 2006
    Posts
    192
    Rep Power
    22
    Yeah, Strings in .NET are immutable - you can't change them without creating a new String. So, if you attempted to change one of the characters, you'll have a new string - but no reference to it.

    What you want to do is declare a char array, and convert the string to a char array. Then you can change the individual characters:
    Code:
    string str = "ahe string";
    char[] c = str.ToCharArray();
    chr[0] = 't';
    
    string newstr = new String(chr); // Convert back to string

  5. #4
    Saint is offline Learning Programmer
    Join Date
    Aug 2006
    Posts
    63
    Rep Power
    0
    Thanks brackett, that worked fine.
    Hi >> Saint

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. string to array
    By lol33d in forum PHP Development
    Replies: 7
    Last Post: 09-10-2011, 11:20 PM
  2. string to array!!!
    By Hamed in forum PHP Development
    Replies: 1
    Last Post: 07-01-2011, 03:20 AM
  3. Add a string to a string array
    By Tannaz in forum C# Programming
    Replies: 1
    Last Post: 12-29-2010, 01:02 PM
  4. Export Array Contents in String Then Read Into Array Later
    By rsnider19 in forum PHP Development
    Replies: 3
    Last Post: 08-20-2010, 02:31 AM
  5. Is string in array?
    By smith in forum Perl
    Replies: 2
    Last Post: 02-14-2007, 11:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts