664
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
The String Class
String
is defined in the
System
namespace. It implements the
IComparable
,
IComparable
,
ICloneable
,
IConvertible
,
IEnumerable
,
IEnumerable
,
and
IEquatable
interfaces.
String
is a sealed class, which means that it cannot be
inherited.
String
provides string-handling functionality for C#. It underlies C#’s built-in
string
type and is part of the .NET Framework. The next few sections examine
String
in detail.
The String Constructors
The
String
class defines several constructors that allow you to construct a string in a variety
of ways. To create a string from a character array, use one of these constructors:
public String(char[ ]
chrs
)
public String(char[ ]
chrs
, int
start
, int
count
)
The first form constructs a string that contains the characters in
chrs.
The second form uses
count
characters from
chrs,
beginning at the index specified by
start.
You can create a string that contains a specific character repeated a number of times
using this constructor:
public String(char
ch
, int
count
)
Here,
ch
specifies the character that will be repeated
count
times.
You can construct a string given a pointer to a character array using one of these
constructors:
public String(char*
chrs
)
public String(char*
chrs
, int
start
, int
count
)
The first form constructs a string that contains the characters pointed to by
chrs.
It is assumed
that
chrs
points to a null-terminated array, which is used in its entirety. The second form uses
count
characters from the array pointed to by
chrs,
beginning at the index specified by
start.
Because they use pointers, these constructors can be used only in unsafe code.
You can construct a string given a pointer to an array of bytes using one of these
constructors:
public String(sbyte*
chrs
)
public String(sbyte*
chrs
, int
start
, int
count
)
public String(sbyte*
chrs
, int
start
, int
count
, Encoding
en
)
The first form constructs a string that contains the bytes pointed to by
chrs.
It is assumed
that
chrs
points to a null-terminated array, which is used in its entirety. The second form
uses
count
characters from the array pointed to by
chrs,
beginning at the index specified by
start.
The third form lets you specify how the bytes are encoded. The default encoding is
ASCIIEncoding
. The
Encoding
class is in the
System.Text
namespace. Because they use
pointers, these constructors can be used only in unsafe code.
www.freepdf-books.com