This documentation is archived and is not being maintained.

String Constructor (Char, Int32)

.NET Framework 1.1

Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.

[Visual Basic]
Public Sub New( _
   ByVal c As Char, _
   ByVal count As Integer _
)
[C#]
public String(
   char c,
 int count
);
[C++]
public: String(
   __wchar_t c,
 int count
);
[JScript]
public function String(
   c : Char,
 count : int
);

Parameters

c
A Unicode character.
count
The number of times c occurs.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException count is less than zero.

Remarks

If count is zero, an Empty instance is initialized.

Example

[C#, C++] The following simple code example demonstrates how you can create an instance of the String class with this constructor.

[C#] 
// Create a Unicode String with 5 Greek Alpha characters
String szGreekAlpha = new String('\u0319',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('\u0319');
// 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.");

[C++] 
// Create a Unicode String with 5 Greek Alpha characters
String* szGreekAlpha = new String(L'\x0319',5);
// Create a Unicode String with a Greek Omega character
wchar_t charArray5 [3] = {L'\x03A9',L'\x03A9',L'\x03A9'};
String* szGreekOmega = new 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'\x0319');
// 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."));

[Visual Basic, JScript] No example is available for Visual Basic or JScript. To view a C# or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String Constructor Overload List | Char | Int32

Show: