none
CString::SetAt equivalent RRS feed

  • Question

  • Is there a method in string class in C# which is equivalent to the CString::SetAt(int nIndex,TCHAR ch ) in MFC?
    Friday, August 10, 2018 6:14 AM

Answers

  • You put the string in System.Text.StringBuilder so it become mutable form, then you can just set the character at specified position.

                string t = "abcdef";
                StringBuilder sb = new StringBuilder(t);
                sb[3] = '3';
                Console.WriteLine(sb.ToString());   //abc3ef

    • Marked as answer by SQLNeophyte Friday, August 10, 2018 11:50 AM
    Friday, August 10, 2018 6:26 AM
    Answerer

All replies

  • You put the string in System.Text.StringBuilder so it become mutable form, then you can just set the character at specified position.

                string t = "abcdef";
                StringBuilder sb = new StringBuilder(t);
                sb[3] = '3';
                Console.WriteLine(sb.ToString());   //abc3ef

    • Marked as answer by SQLNeophyte Friday, August 10, 2018 11:50 AM
    Friday, August 10, 2018 6:26 AM
    Answerer
  • Hi cheong00,

    Thanks for your quick reply. One more think I would like to re-confirm is that it means there is no equivalent method in string class for this operation right?

    Friday, August 10, 2018 7:10 AM
  • Yes.

    String in C# are immutable, so you cannot alter part of it once the value is set.

    Friday, August 10, 2018 9:56 AM
    Answerer
  • Ok..That is clear.. thanks a lot for the support..
    Friday, August 10, 2018 11:50 AM