C# String Theory—String constructors

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

C# String Theory

edit

String constructors

Most of the String class constructors are intended for use in system software and are unsafe; so, we will not cover them in this article. The following are String constructors which are useful in non-system programming.

The first constructor initializes a new instance of the String class with the value indicated by an array of Unicode characters. If the value is null or empty, an Empty instance is initialized.

// public String (char[] value);
 
String charString = 
   new String (new char [] {'A','B','\u0043'});
Console.WriteLine (charString); // Outputs "ABC" 

The second constructor specifies a single char and a count for the number of times the character is to be repeated in the new string. If count is zero, an Empty instance is initialized.

// public String (char c, int count);
 
String atString = new String ('\u0040',3);
Console.WriteLine (atString);                  // Outputs "@@@" 

The third constructor copies Unicode characters from value, starting at startIndex and ending at (startIndex + length - 1). If length is zero, an Empty instance is initialized.

//public String (char[] value, int startIndex, int length);
 
String aString   = new String ('A', 1);
String abcString = new String 
   (new char [] {'\u0041','\u0042','\u0043'}, 1, 2);
Console.WriteLine (aString + abcString);       // Outputs "ABC" 


Previous_Page_.gif Next_Page_.gif


Personal tools