1

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#.

2 Answers 2

3

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());
Sign up to request clarification or add additional context in comments.

1 Comment

Note that this would add the character at that position, not replace the character at that position with a new one.
2

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.