String Data Type

Modified on 2009/01/13 17:39 by Administrator — Categorized as: Uncategorized

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 = "";

String ( String )

Initializes a new String instance to the specified value.

Syntax:

String( String value )

Parameters:

value: A String.

Example:


s = new String( "abc" );
// is equivalant to
s = "abc";

String ( Number )

Initializes a new String instance to the string value of the number parameter.

Syntax:

String( Number value )

Parameters:

value: A Number.

Example:


s = new String( 12 ); // results in "12"

String ( Boolean )

Initializes a new String instance to the string value of the Boolean parameter.

Syntax:

String( Boolean value )

Parameters:

value: A Boolean.

Example:


b = true;
s = new String( b ); // results in "true"

String ( DateTime )

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:

String( DateTime value )

Parameters:

value: A DateTime.

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: Number

Example:


s = "hello";
len = s.Length; // result in 5

Instance Methods

Contains

Determines whether the this instance contains the specified String.

Syntax:

Boolean Contains(String sought)

Parameters:

sought: A string.

Return Value

true 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:

String End(Number count)

Parameters

count: The number of characters to retrieve from the end of this instance.

Return Value

A 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:

Boolean EndsWith(String sought)

Parameters:

sought: A string.

Return Value

true 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:

Number IndexOf(String sought)

Parameters:

sought: The String to seek.

Return Value

The 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:

Number IndexOf(String sought, Number startIndex)

Parameters:

sought: The String to seek.

startIndex: The search starting position.

Return Value

The 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:

Number IndexOf(String sought, Number startIndex, Number count)

Parameters:

sought: The String to seek.

startIndex: The search starting position.

count: The number of character positions to examine.

Return Value

The 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:

String Insert(Number startIndex, String value)

Parameters:

startIndex: The index position of the insertion.

value: The String to insert.

Return Value

A 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:

String Remove(Number startIndex)

Parameters:

startIndex: The position to begin deleting characters.

Return Value

A 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:

String Remove(Number startIndex, Number count)

Parameters:

startIndex: The position in this instance to begin deleting characters.

count: The number of characters to delete.

Return Value

A 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:

String RemoveEnd(Number count)

Parameters

count: The number of characters to remove from the end of this instance.

Return Value

A 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:

String RemoveStart(Number count)

Parameters

count: The number of characters to remove from the start of this instance.

Return Value

A 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:

String Replace(String oldValue, String newValue)

Parameters:

oldValue: A String to be replaced.

newValue: A String to replace all occurrences of oldValue.

Return Value

A 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:

String Start(Number count)

Parameters

count: The number of characters to retrieve from the start of this instance.

Return Value

A 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:

Boolean StartsWith(String sought)

Parameters:

sought: The String to seek.

Return Value

true 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:

String Substring(Number startIndex)

Parameters:

startIndex: The starting character position of a substring in this instance.

Return Value

A 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:

String Substring(Number startIndex, Number length)

Parameters:

startIndex: The starting character position of a substring in this instance.

length: The number of characters in the substring.

Return Value

A 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:

String ToFriendlyName()

Return Value

The 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:

String ToLower()

Return Value

A String in lowercase.

Example:


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:

String ToTitleCase()

Return Value

A String in titlecase.

Example:


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:

String ToUpper()

Return Value

A String in uppercase.

Example:


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:

String Trim()

Return Value

A 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"