. Advertisement .
..3..
. Advertisement .
..4..
There is no shortage of ways to convert string array to string in C#. Learn more about these solutions and how you can implement them in your program.
Convert String Array To String In C#
With string()
In C#, strings are actually sequences of characters encoded by UTF-16 by default to represent text. In particular, each instance of the String class is a collection of Char objects, in which each Char object is equated to a UTF-16 code unit. The content of this Char object collection is also the value of the corresponding String object.
There are plenty of ways to instantiate a String object in C#. You can assign a string literal to a String variable, call the String class constructor, use the string concatenation operator, and many other options.
In this case, the String class constructor can help us convert a character array into a string. In fact, this is one of the common ways to create a string in C#.
Syntax: string str = new string(char_array);
The statement above will create a new String object from the character array char_array. This is an example of how to use it in practice:
using System;
public class ITTutoria{
static void Main(string[] args)
{
char[] arr = {
'I', 'T', 'T', 'u',
't', 'o', 'r', 'i',
'a'};
string str = new string(arr);
Console.WriteLine(str);
}
}
Output:
ITTutoria
With join()
The join()
method of the string class can concatenate all elements of an array, especially when there are separators between those elements. This method can also apply to a collection, where it will concatenate its members.
The most popular overload of this method is Join(String, Object[])
with the syntax:
public static string Join (string? separator, params object?[] values);
Parameters:
- values: the array containing the elements you need to concatenate.
- separator: a string that will be used as the separator. However, not every returned string includes it. The method only does so when there is more than one element in it.
The join()
method returns a string that has every element of the array values, separated by the separator parameter. If the original array has no elements, the method returns String.Empty
.
The following example uses this method to create a string from the same character array as above. Pay attention to the separator, which is “”. It means you don’t want to insert anything between characters in the final string.
using System;
public class ITTutoria{
static void Main(string[] args)
{
char[] arr = {
'I', 'T', 'T', 'u',
't', 'o', 'r', 'i',
'a'};
string str = string.Join("", arr);
Console.WriteLine(str);
}
}
Output:
ITTutoria
With concat()
The concat()
method can concatenate multiple String objects or representations of multiple instances of Objects, including System.Char
.
In this case, we need to concatenate the string representations of all the elements of a Char array with this syntax:
public static string Concat (params object?[] args);
In which args is the object array that stores the elements you need to concatenate. This is how you can rewrite all those solutions with concat()
:
using System;
public class ITTutoria{
static void Main(string[] args)
{
char[] arr = {
'I', 'T', 'T', 'u',
't', 'o', 'r', 'i',
'a'};
string str = string.Concat(arr);
Console.WriteLine(str);
}
}
Output:
ITTutoria
Conclusion
You can use plenty of methods and functions to convert string array to string in C#: string()
constructor, jon()
, and concat()
. All of them are members of the String class, which is responsible for creating and manipulating strings in this programming language. And if you want to convert this string to float next, follow this guide.
Leave a comment