Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.
Assembly: mscorlib (in mscorlib.dll)
Public Sub New ( _ c As Char, _ count As Integer _ )
public String( char c, int count )
public: String( wchar_t c, int count )
new : c:char * count:int -> String
Parameters
- c
- Type: System.Char
A Unicode character.
- count
- Type: System.Int32
The number of times c occurs.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException |
count is less than zero. |
If count is zero, an Empty instance is initialized.
The following simple example demonstrates how you can create an instance of the String class with this constructor.
' Create a Unicode String with 5 Greek Alpha characters Dim szGreekAlpha As New String(ChrW(&H0391), 5) ' Create a Unicode String with a Greek Omega character Dim szGreekOmega As New String(New Char() {ChrW(&H03A9), ChrW(&H03A9), _ ChrW(&H03A9)}, 2, 1) Dim szGreekLetters As String = String.Concat(szGreekOmega, szGreekAlpha, _ szGreekOmega.Clone()) ' Examine the result Console.WriteLine(szGreekLetters) ' The first index of Alpha Dim iAlpha As Integer = szGreekLetters.IndexOf(ChrW(&H0391)) ' The last index of Omega Dim iomega As Integer = szGreekLetters.LastIndexOf(ChrW(&H03A9)) Console.WriteLine("The Greek letter Alpha first appears at index {0}.", _ ialpha) Console.WriteLIne("The Greek letter Omega last appears at index {0}.", _ iomega)
// Create a Unicode String with 5 Greek Alpha characters String szGreekAlpha = new String('\u0391',5); // Create a Unicode String with a Greek Omega character String szGreekOmega = new String(new char [] {'\u03A9','\u03A9','\u03A9'},2,1); String szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega.Clone()); // Examine the result Console.WriteLine(szGreekLetters); // The first index of Alpha int ialpha = szGreekLetters.IndexOf('\u0391'); // The last index of Omega int iomega = szGreekLetters.LastIndexOf('\u03A9'); Console.WriteLine("The Greek letter Alpha first appears at index " + ialpha + " and Omega last appears at index " + iomega + " in this String.");
// Create a Unicode String with 5 Greek Alpha characters String^ szGreekAlpha = gcnew String( L'\x0391',5 ); // Create a Unicode String with a Greek Omega character wchar_t charArray5[3] = {L'\x03A9',L'\x03A9',L'\x03A9'}; String^ szGreekOmega = gcnew String( charArray5,2,1 ); String^ szGreekLetters = String::Concat( szGreekOmega, szGreekAlpha, szGreekOmega->Clone() ); // Examine the result Console::WriteLine( szGreekLetters ); // The first index of Alpha int ialpha = szGreekLetters->IndexOf( L'\x0391' ); // The last index of Omega int iomega = szGreekLetters->LastIndexOf( L'\x03A9' ); Console::WriteLine( String::Concat( "The Greek letter Alpha first appears at index ", Convert::ToString( ialpha ) ) ); Console::WriteLine( String::Concat( " and Omega last appears at index ", Convert::ToString( iomega ), " in this String." ) );
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Reference
A string of characters can be created with the New-Object cmdlet
New-Object String('=',42)
or the more complete statement
New-Object System.String('=',42)
The example above on Greek characters can be done like this in PowerShell
New-Object System.String([System.Char]0x0319,42)
but unfortunately the PowerShell console can't show unicode characters, so all you will be shown is a line of square characters.