HomeSearch

C# String Constructor (new string)

These C# examples use the string constructor to create strings. Char arrays and repeated characters are used.

String constructor.

A string constructor call is normally not needed. But the C# language provides many string constructors. These create strings from characters and arrays.Strings

One possible use

for a string constructor is creating a string containing one char repeated many times. Another is converting a char array into a string.ConvertChar Array

Char array example.

Here we create a string with a string constructor from an existing char array. The string "abc" results from a 3-char array.

Convert: The string constructor is an ideal way to convert a char array to a string.

Convert Char Array, String

Tip: To create a new string, you usually should just modify an existing string or use a string literal.

String Literal
C# program that uses string constructor, char array using System; class Program { static void Main() { // Create new string from char array. char[] charArray = new char[3]; charArray[0] = 'a'; charArray[1] = 'b'; charArray[2] = 'c'; string exampleString = new string(charArray); Console.WriteLine(exampleString); Console.WriteLine(exampleString == "abc"); } } Output abc True

Repeated chars.

Suppose we want to create a whitespace string with a space repeated 100 times. This is easy with the string constructor—no large string literal is needed.

Here: We repeat the letter "A" 10 times. The resulting string can be used like any other in a C# program.

PadRight: This is much simpler than using PadRight or PadLeft methods to create repeated characters.

PadRight, PadLeft
C# program that uses repeated characters using System; class Program { static void Main() { // Create new string of repeated characters. string exampleString = new string('a', 10); Console.WriteLine(exampleString); Console.WriteLine(exampleString == "aaaaaaaaaa"); } } Output aaaaaaaaaa True

Char array, range.

Suppose we have a char array, but want to create a string from just part of it. We can use another string constructor and specify and offset or a length.

Here: We start at index 0, and continue until we have taken 3 chars. We turn the first 3 chars of the array into a string.

Substring: This string constructor is the equivalent of Substring, but acts on char arrays.

Substring
C# program that uses char array, range for string using System; class Program { static void Main() { // Create new string from range of characters in array. char[] charArray = new char[6]; charArray[0] = 'a'; charArray[1] = 'B'; charArray[2] = 'c'; charArray[3] = 'D'; charArray[4] = 'e'; charArray[5] = 'F'; string exampleString = new string(charArray, 0, 3); Console.WriteLine(exampleString); Console.WriteLine(exampleString == "aBc"); } } Output aBc True

Constructors.

We look at the 8 string constructors in the .NET Framework. You can see that 5 of the constructors are unsafe. Unsafe code should usually be avoided.
String constructors: C# unsafe public String(char*); public String(char[]); unsafe public String(sbyte*); public String(char, int); unsafe public String(char*, int, int); public String(char[], int, int); unsafe public String(sbyte*, int, int); unsafe public String(sbyte*, int, int, Encoding);

Notes.

This page doesn't demonstrate the unsafe constructors, as your code should avoid unsafe methods. You might need these 5 unsafe methods for DLL interop.

Tip: There is no difference between calling "new String" and "new string". The uppercase first letter makes no difference.

Optimization.

We can use the string constructors as an impressive optimization. Sometimes, we can entirely replace StringBuilder with a char array.

And: This is most useful for sorting algorithms or other lower-level operations on strings in C# programs.

StringBuilder

Optimization, repeat chars.

We can optimize the string constructor that repeats characters N times to a string literal. In a benchmark, the literal is faster (it is already generated).

A summary.

String constructors are occasionally useful. We used the 3 string constructors in C# that are not unsafe. And we found ways string conversions can be optimized.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.