开发者

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

开发者 https://www.devze.com 2023-01-21 07:54 出处:网络
i have a string strText with certain value on it,i need to assign \'\\0\' or charactora开发者_运维百科t the specified position of strText.

i have a string strText with certain value on it,i need to assign '\0' or charactor a开发者_运维百科t 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消