Represents text; that is, a series of characters (letters, numbers, symbols, etc).
String character positions are zero base, ie. the first character in a string is at index 0.
Constructors
String ( )
Initializes a new String instance an empty string. ( "" )
Syntax:String()
Example:
s = new String();
// is equivalant to
s = "";
Initializes a new String instance to the specified value.
Syntax:Parameters:Example:
s = new String( "abc" );
// is equivalant to
s = "abc";
Initializes a new String instance to the string value of the number parameter.
Syntax:Parameters:Example:
s = new String( 12 ); // results in "12"
Initializes a new String instance to the string value of the
Boolean parameter.
Syntax:Parameters:Example:
b = true;
s = new String( b ); // results in "true"
Initializes a new String instance to the string value of the
DateTime parameter. It is recommended that you use the
DateTime.ToString( ) method instead.
Syntax:Parameters:Example:
dt = new DateTime();
s = DateTime( dt ); // results in "1/12/2008", format depends on current locale.
Instance Properties
Length
Gets the number of characters in the string.
Type:
NumberExample:
s = "hello";
len = s.Length; // result in 5
Instance Methods
Contains
Determines whether the this instance contains the specified
String.
Syntax:Parameters:sought: A string.
Return Valuetrue if this instance contains sought; otherwise, false.
Example:
s = "abc";
b = s.Contains("b"); // results in true
End
Retrieve the specified number of characters from the end of this instance.
Syntax:Parameterscount: The number of characters to retrieve from the end of this instance.
Return ValueA new
String containing the specified nnumber of characters from the end of this instance.
Example:
s1 = "abc";
s2 = s1.End(2); // results in "bc"
EndsWith
Determines whether the end of this instance matches the specified
String.
Syntax:Parameters:sought: A string.
Return Valuetrue if the end of this instance matches value; otherwise, false.
Example:
s = "abc";
b = s.EndsWith("bc"); // results in true
IndexOf ( String )
Reports the index of the first occurrence of the specified
String in this instance.
Syntax:Parameters:Return ValueThe index position of
sought if that
String is found, or -1 if it is not. If value is Empty, the return value is 0.
Example:
s = "abc";
i = s.IndexOf("abc"); // results in 1
IndexOf ( String, Number )
Reports the index of the first occurrence of the specified
String in this instance. The search starts at a specified character position.
Syntax:Parameters:startIndex: The search starting position.
Return ValueThe index position of
sought if that
String is found, or -1 if it is not. If value is Empty, the return value is
startIndex.
Example:
s = "abc";
i = s.IndexOf("abc", 1); // results in 1
i = s.IndexOf("abc", 2); // results in -1
IndexOf ( String, Number, Number )
Reports the index of the first occurrence of the specified
String in this instance. The search starts at a specified character position and examines a specified number of character positions.
Syntax:Parameters:startIndex: The search starting position.
count: The number of character positions to examine.
Return ValueThe index position of
sought if that
String is found, or -1 if it is not. If
value is Empty, the return value is
startIndex.
Example:
s = "abc";
i = s.IndexOf("abc", 0, 1); // results in -1
i = s.IndexOf("abc", 1, 2); // results in 1
Insert
Inserts a specified instance of
String at a specified index position in this instance.
Syntax:Parameters:startIndex: The index position of the insertion.
Return ValueA new
String equivalent to this instance but with
value inserted at position
startIndex.
Example:
s1 = "abc";
s2 = s1.Insert("XX", 1); // results in "aXXbc"
Remove ( Number )
Deletes a specified number of characters starting at a specified position.
Syntax:Parameters:startIndex: The position to begin deleting characters.
Return ValueA new
String that is equivalent to this instance up to
startIndex.
Example:
s1 = "abc";
s2 = s1.Remove(2); // results in "ab"
Remove ( Number, Number )
Deletes a specified number of characters from this instance beginning at a specified position.
Syntax:Parameters:startIndex: The position in this instance to begin deleting characters.
count: The number of characters to delete.
Return ValueA new
String that is equivalent to this instance less
count number of characters.
Example:
s1 = "abc";
s2 = s1.Remove(1, 1); // results in "ac"
RemoveEnd
Removes the specified number of characters from the end of this instance.
Syntax:Parameterscount: The number of characters to remove from the end of this instance.
Return ValueA new
String equivalent to this instance after removing the specified number of characters from the end.
Example:
s1 = "abc";
s2 = s1.RemoveEnd(2); // results in "a"
RemoveStart
Removes the specified number of characters from the start of this instance.
Syntax:Parameterscount: The number of characters to remove from the start of this instance.
Return ValueA new
String equivalent to this instance after removing the specified number of characters from the start.
Example:
s1 = "abc";
s2 = s1.RemoveStart(2); // results in "c"
Replace
Replaces all occurrences of a specified
String in this instance with another specified
String.
Syntax:Parameters:oldValue: A
String to be replaced.
newValue: A
String to replace all occurrences of oldValue.
Return ValueA
String equivalent to this instance but with all instances of
oldValue replaced with
newValue.
Example:
s1 = "abc";
s2 = s1.Replace("bc", "XY"); // results in "aXY"
Start
Retrieve the specified number of characters from the start of this instance.
Syntax:Parameterscount: The number of characters to retrieve from the start of this instance.
Return ValueA new
String containing the specified nnumber of characters from the start of this instance.
Example:
s1 = "abc";
s2 = s1.Start(2); // results in "ab"
StartsWith
Determines whether the beginning of this instance matches the specified
String.
Syntax:Parameters:Return Valuetrue if the end of this instance matches sought; otherwise, false.
Example:
s1 = "abc";
s2 = s1.StartsWith("abc", "ab"); // results in true
Substring ( Number )
Retrieves a substring from this instance. The substring starts at a specified character position.
Syntax:Parameters:startIndex: The starting character position of a substring in this instance.
Return ValueA
String equivalent to the substring that begins at
startIndex in this instance.
-or-
An empty
String if
startIndex is equal to the length of this instance.
Example:
s1 = "abc";
s2 = s1.Substring(1); // results in "bc"
Substring ( Number, Number )
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
Syntax:Parameters:startIndex: The starting character position of a substring in this instance.
length: The number of characters in the substring.
Return ValueA
String equivalent to the substring of length length that begins at
startIndex in this instance.
-or-
An empty
String if
startIndex is equal to the length of this instance and
length is zero.
Example:
s1 = "abc";
s2 = s1.Substring(1, 1); // results in "b"
ToFriendlyName
Returns the friendly name of this
String. The friendly name of a string adds a space before each upper cased letter in the string where a space did not previously exist.
Syntax:Return ValueThe friendly name of this
String.
Example:
s1 = "ATaleOfTwoCities";
s2 = s1.ToFriendlyName(); // results in "A Tale Of Two Cities"
ToLower
Returns a copy of this
String in lowercase.
Syntax:Return ValueExample:
s1 = "Abc";
s2 = s1.ToLower(); // results in "abc"
ToTitleCase
Returns a copy of this
String in titlecase. Generally, title casing converts the first character of a word to uppercase and the rest of the characters to lowercase. However, a word that is entirely uppercase, such as an acronym, is not converted.
Syntax:Return ValueExample:
s1 = "A tale of two cities";
s2 = s1.ToTitleCase(); // results in "A Tale Of Two Cities"
ToUpper
Returns a copy of this
String in uppercase.
Syntax:Return ValueExample:
s1 = "Abc";
s2 = s1.ToUpper(); // results in "ABC"
Trim
Removes all occurrences of white space characters from the beginning and end of this instance.
Syntax:Return ValueA new
String equivalent to this instance after white space characters are removed from the beginning and end.
Example:
s1 = " abc ";
s2 = s1.Trim(); // results in "abc"
Examples
len = new String("abc").Length; // results in 3
s = new String("abc");
len = s.Length; // results in 3
s = "abc";
pos = s.IndexOf("b"); // results in 1
s = "abc";
pos = s.Substring(1, 2); // results in "bc"
s = " abc ";
s2 = s.Trim(); // results in "abc"