Home  >  Article  >  Backend Development  >  Detailed explanation of the method of converting char character array and string in C#

Detailed explanation of the method of converting char character array and string in C#

黄舟
黄舟Original
2017-03-08 11:17:192366browse

This article mainly introduces the method of converting char character arrays and strings in C#, and briefly analyzes the specific implementation skills of C# character arrays to strings and strings to character arrays in the form of examples. Friends who need it can refer to it. Next

The example of this article describes the method of converting char character array and string in C#. Share it with everyone for your reference, the details are as follows:

1. Convert string to character array

char[] tempChar = sourceString.ToCharArray();

2. Convert the character array to a string

//方法一
string str = string.Join("", tempChar);
//方法二
string str = string.Concat<char>(tempChar);
//方法三
string str = new string(tempChar);

Note: Method 1 uses a clever way because the original purpose of Join It is to concatenate the members in the collection and use the specified delimiter between each member. Here, the delimiter is specified as an empty string, so that the output effect is converted from a character array to a string. Method 2. The Concat method is used to concatenate the members of the enumerable interface implementation in the collection, and it can be used here. Method three uses the construction method of the string class, which is recommended.


The above is the detailed content of Detailed explanation of the method of converting char character array and string in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn