Characters are the fundamental building blocks of C# program code. Every program is composed of a sequence of characters that’s interpreted by the compiler as a series of instructions used to accomplish a task.
A string
is a series of characters treated as a single unit. A string
may include letters, digits and the various special characters: +
, -
, *
, /
, $
and others.
string
ConstructorsAll string
s are immutable—their character contents cannot be changed after they’re created.
string
Indexer, Length
Property and CopyTo
MethodP
roperty Length
determines the number of characters in a string
.
The string
indexer receives an integer argument as the position number and returns the character at that position. The first element of a string
is considered to be at position 0.
Attempting to access a character that’s outside a string
’s bounds results in an IndexOutOfRange-Exception
.
Method CopyTo
copies a specified number of characters from a string
into a char
array.
string
sSome string
-comparison capabilities use word sorting rules that depend on the computer’s currently selected culture.
Method Equals
and the overloaded equality operator (==
) can each be used to compare the contents of two string
s for equality.
Method CompareTo
returns 0
if the string
s are equal, a negative number if the string
that invokes CompareTo
is less than the string
passed as an argument and a positive number if the string
that invokes CompareTo
is greater than the string
passed as an argument.
string
methods StartsWith
and EndsWith
determine whether a string
starts or ends with the characters specified as an argument, respectively.
string
sstring
method IndexOf
locates the first occurrence of a character or a substring in a string
. Method LastIndexOf
locates the last occurrence of a character or a substring in a string
.
string
sClass string
provides two Substring
methods to enable a new string
to be created by copying part of an existing string
.
string
sThe static
method Concat
of class string
concatenates two string
s and returns a new string
containing the characters from both original string
s.
StringBuilder
Once a string
is created, its contents can never change. Class StringBuilder
(namespace System.Text
) is available for creating and manipulating string
s that can change.
Length
and Capacity
Properties, EnsureCapacity
Method and Indexer of Class StringBuilder
Class StringBuilder
provides Length
and Capacity
properties to return, respectively, the number of characters currently in a StringBuilder
and the number of characters that can be stored in a StringBuilder
without allocating more memory. These properties also can be used to increase or decrease the StringBuilder
’s length or the capacity.
Method EnsureCapacity
allows you to guarantee that a StringBuilder
has a minimum capacity.
Append
and AppendFormat
Methods of Class StringBuilder
Class StringBuilder
provides Append
and AppendFormat
methods to allow various types of values to be added to the end of a StringBuilder
.
Insert
, Remove
and Replace
Methods of Class StringBuilder
Class StringBuilder
provides many overloaded Insert
methods to allow various types of values to be inserted at any position in a StringBuilder
. Versions are provided for each simple type and for character arrays, string
s and Object
s.
Class StringBuilder
also provides method Remove
for deleting any portion of a StringBuilder
.
StringBuilder
method Replace
searches for a specified string
or character and substitutes another in its place.
Char
MethodsThe simple types are actually aliases for struct
types.
All struct
types derive from class ValueType
, which in turn derives from object
.
All struct
types are implicitly sealed
.
Char
is a struct that represents characters.
Method Char.IsDigit
determines whether a character is a defined Unicode digit.
Method Char.IsLetter
determines whether a character is a letter.
Method Char.IsLetterOrDigit
determines whether a character is a letter or a digit.
Method Char.IsLower
determines whether a character is a lowercase letter.
Method Char.IsUpper
determines whether a character is an uppercase letter.
Method Char.ToUpper
converts a lowercase character to its uppercase equivalent.
Method Char.ToLower
converts an uppercase character to its lowercase equivalent.
Method Char.IsPunctuation
determines whether a character is a punctuation mark.
Method Char.IsSymbol
determines whether a character is a symbol.
Method Char.IsWhiteSpace
determines whether a character is a whitespace character.
Method Char
.CompareTo
compares two character values.