C Realization of char Character Array and String Conversion Method

  • 2021-12-04 19:27:03
  • OfStack

In this paper, an example of C # to achieve char character array and string conversion method. Share it for your reference, as follows:

1. Convert a string to an array of characters


char[] tempChar = sourceString.ToCharArray();

2. Convert an array of characters to a string


// Method 1
string str = string.Join("", tempChar);
// Method 2
string str = string.Concat<char>(tempChar);
// Method 3
string str = new string(tempChar);

Note: Method 1 uses a clever way, because the original purpose of Join is to concatenate the members in the collection and adopt the specified separator between each member. Here, the separator is specified as an empty string, thus realizing the conversion from character array to string in output effect. Method 2. The Concat method is used to concatenate members of an enumerable interface implementation within a collection, and is available here. Method 3. The construction method of string class is used, which is recommended.

For more readers interested in C # related contents, please check the topics on this site: "Summary of C # String Operation Skills", "Summary of C # Array Operation Skills", "Summary of XML File Operation Skills in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: