简体   繁体   中英

how to assign a char value to string variable in c#

i have a string strText with certain value on it,i need to assign '\\0' or charactor at the specified position of strText. ie strText[5]='\\0'.how is it possible in c#.

You can use the Insert method to specify the index. You need to give it a string though, so if you can replace '\\0' with "\\0" or else just call .ToString()

strText = strText.Insert(5, yourChar.ToString());

Strings are immutable, so you will need to convert it to a character array, set the character at the specified position, and then convert back to string:

char[] characters = "ABCDEFG".ToCharArray ();
characters[5] = '\0';
string foo = new String (characters);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM