C# string and char[] convert each other

In C#, the conversion between strings and character arrays can be realized by converting between stringand .char[]

Convert stringto char[]:

string str = "hello";
char[] charArray = str.ToCharArray();

Convert char[]to string:

char[] charArray = new char[] {'h', 'e', 'l', 'l', 'o'};
string str = new string(charArray);

It should be noted that during the conversion process, new objects will be created and new memory will be allocated, so these conversion operations need to be used with caution when dealing with large amounts of data.

Guess you like

Origin blog.csdn.net/qq_60125117/article/details/130252896