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#.
asked Oct 21, 2010 at 17:55
Sudheesh A P
773 silver badges9 bronze badges
2 Answers 2
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());
answered Oct 21, 2010 at 17:58
Brandon
70.3k32 gold badges201 silver badges227 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Steve Ellinger
Note that this would add the character at that position, not replace the character at that position with a new one.
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);
answered Oct 21, 2010 at 17:58
Pete
11.6k4 gold badges46 silver badges55 bronze badges
Comments
lang-cs