简体   繁体   English

如何在C#中将char值分配给字符串变量

[英]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. 我有一个带有特定值的字符串strText,我需要在strText的指定位置分配“ \\ 0”或字符。 ie strText[5]='\\0'.how is it possible in c#. 即strText [5] ='\\ 0'。在c#中怎么可能。

You can use the Insert method to specify the index. 您可以使用Insert方法指定索引。 You need to give it a string though, so if you can replace '\\0' with "\\0" or else just call .ToString() 不过,您需要给它一个字符串,因此,如果可以将“ \\ 0”替换为“ \\ 0”,否则只需调用.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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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