String functions

Last updated Sunday, November 25, 2001.

Alignment, center-justify

Given a string myString and a desired result width width, a center-justify function returns a new string that is length characters long containing myString padded as evenly as possible on either side with space characters. For example, a center-justify function, given the string "Rosetta Stone" (which contains 13 characters) and a desired result width of 23, should yield the result "     Rosetta Stone     " (5 spaces, followed by "Rosetta Stone", followed by 5 more spaces).

C#

C#'s string class doesn't have a method for center-justification. I'll post some code soon.

MFC's CString class (C++)

MFC's CString class doesn't have a method for center-justification. I'll post some code soon.

Python

center function

from string import center

center(myString, width)

STL's string class (C++)

MFC's string class doesn't have a method for center-justification. I'll post some code soon.

RB

RB doesn't have a built-in function for center-justification. I'll post some code soon.

VB 6

VB 6 doesn't have a built-in function for center-justification. I'll post some code soon.

VB .NET

VB .NET doesn't have a built-in function for center-justification. I'll post some code soon.

 

Alignment, left-justify

Given a string myString and a desired result width width, a left-justify function returns a new string that is length characters long containing the given string padded on the right with space characters. For example, a left-justify function, given the string "Rosetta Stone" (which contains 13 characters) and a desired result width of 23, should yield the result "Rosetta Stone          " ("Rosetta Stone" followed by 10 spaces).

C#

PadRight method

myString.PadRight(width)

MFC's CString class (C++)

TODO: Fill this in.

Python

ljust function

from string import ljust

ljust(myString, width)

STL's string class (C++)

TODO: Fill this in.

RB

Use custom function: LeftJustify

function LeftJustify(myString as string, width as integer) as string

    dim rightPadding as string
    dim i as Integer

    for i = 1 to width - len(myString)
       
rightPadding = rightPadding + chr(32)
    next


   
return left(myString, width) + rightPadding

end function

VB 6

Use custom function: LeftJustify

function LeftJustify(myString as string, width as integer) as string

    dim rightPadding as string
    dim i as Integer

    for i = 1 to width - len(myString)
       
rightPadding = rightPadding + chr(32)
    next


   
LeftJustify = left(myString, width) + rightPadding

end function

VB .NET

PadRight method

myString.PadRight(width)

 

LSet function

LSet(myString, width)

 

Alignment, right-justify

Given a string myString and a desired result width width, a right-justify function returns a new string that is length characters long containing the given string padded on the left with space characters For example, a right-justify function, given the string "Rosetta Stone" (which contains 13 characters) and a desired result width of 23, should yield the result "          Rosetta Stone" (10 spaces followed by "Rosetta Stone").

C#

PadLeft method

myString.PadLeft(width)

MFC's CString class (C++)

TODO: Fill this in.

Python

ljust function

from string import rjust

rjust(myString, width)

STL's string class (C++)

TODO: Fill this in.

RB

Use custom function: RightJustify

function RightJustify(myString as string, width as integer) as string

    dim leftPadding as string
    dim i as Integer

    for i = 1 to width - len(myString)
       
leftPadding = leftPadding + chr(32)
    next


   
return leftPadding + left(myString, width)

end function

VB 6

Use custom function: RightJustify

function RightJustify(myString as string, width as integer) as string

    dim leftPadding as string
    dim i as Integer

    for i = 1 to width - len(myString)
       
leftPadding = leftPadding + chr(32)
    next


   
RightJustify = leftPadding + left(myString, width)

end function

VB .NET

PadRight method

myString.PadRight(width)

 

RSet function

RSet(myString, width)

 

Array/list: array/list to string

Given an array of strings myArray (or in Python, a list of strings myList) and a separator character separator, an array/list-to-string function returns a string made of all the strings in myArray, separated by the separator. For example, consider the array (or list) illustrated by the table below:

element string
0 Welcome
1 to
2 the
3 Rosetta
4 Stone

This array, if provided with " " (a single space character) as the separator character to the array/list-to-string function, should return the string "Welcome to the Rosetta Stone".

C#

Join method

String.Join(separator, myArray)

MFC's CString class (C++)

TODO: Fill this in.

Python

join / joinfields functions

join(myList[, separator])

joinfields(myList[, separator])

myList can be either a list or a tuple. If separator is not provided, the space character is used by default.

join and joinfields now do the same thing. In the past, these functions took different numbers of arguments.

STL's string class (C++)

TODO: Fill this in.

RB

TODO: Fill this in.

VB 6

Join function

Join(myArray[, separator])

VB .NET

Join method

String.Join(separator, myArray)

 

Join function

Join(myArray[, separator])

 

Array/list: string to array/list

Given a string myString and one or more separator characters, an string-to-array/list function returns an array of strings derived from substrings of myString delimited by the separator characters. For example, given the string "Welcome to the Rosetta Stone". The string, if provided with " " (a single space) as the separator character, should yield an array illustrated by the table below.

element string
0 Welcome
1 to
2 the
3 Rosetta
4 Stone

 

C#

Split method

myString.Split([separatorArray])

MFC's CString class (C++)

TODO: Fill this in.

Python

split / splitfields functions

split(myString[, separator[, maxsplit]])

splitfields(myString[, separator[, maxsplit]])

split() and splitfields() now do the same thing. In the past, these functions took different numbers of arguments.

STL's string class (C++)

TODO: Fill this in.

RB

No equivalent.

VB6

Split function

Split(myString [, separator [, count[, compare]]])

VB .NET

Split method

myString.Split([separatorArray])

 

Split function

Split(myString [, separator [, count[, compare]]])

 

Case, convert to lower

Given a string myString, a lowercase conversion function returns a new string containing the myString, with all alphabetical characters converted to lowercase. For example, given the string "The ROSETTA Stone", it should return "the rosetta stone".

C#

ToLower

myString.ToLower()

MFC's CString class (C++)

TODO: Fill this in.

Python

lower

from string import lower

lower(myString)

Returns a string containing the contents of myString, with all uppercase characters converted to lowercase.

STL's string class (C++)

TODO: Fill this in.

RB

lowercase

lowercase(myString)

Returns a string containing the contents of myString, with all uppercase characters converted to lowercase.

VB 6

LCase / LCase$

LCase(myString)

LCase$(myString)

LCase returns a variant (string) containing the contents of myString, with all uppercase characters converted to uppercase.

LCase$ does the same thing as LCase, except that it returns a string value instead of a string value contained within a variant.

 

StrConv

StrConv(myString, vbLowerCase)

StrConv is a general-purpose string conversion function that can be used to convert to lowercase, among other things. vbLowerCase is a pre-defined constant whose value is 2. But really, why use this function when you've got LCase or LCase$, which are more readable and take only one argument?

VB .NET

ToLower

myString.ToLower()

 

LCase / LCase$

LCase(myString)

LCase$(myString)

LCase returns a variant (string) containing the contents of myString, with all uppercase characters converted to uppercase.

LCase$ does the same thing as LCase, except that it returns a string value instead of a string value contained within a variant.

 

StrConv

StrConv(myString, VbStrConv.LowerCase)

StrConv is a general-purpose string conversion function that can be used to convert to uppercase, among other things. VbStrConv.LowerCase is a pre-defined constant. But really, why use this function when you've got ToLower, which is more readable, standard across all CLR langauges and takes only one argument?

 

Case, convert first letter of each word to upper

Given a string myString, a "convert first letter of each word to uppercase" function (also called a "propercase" or "capitalize" function) returns a new string containing myString, with the first characters in each word converted to uppercase, if applicable. For example, given the string "the rosetta stone", it should return "The Rosetta Stone".

C#

 

MFC's CString class (C++)

TODO: Fill this in.

Python

capwords function

from string import capwords

capwords(myString)

capwords replaces runs of whitespace characters with a single space and removes leading and trailing whitespace. It does so because it performs the capitalization by splitting myString into words using split, capitalizing each word using capitalize, and then joining the capitalized words using join.

STL's string class (C++)

TODO: Fill this in.

RB

Titlecase function

Titlecase(phrase as String)

Note that the only valid word separator for the Titlecase function is the space (character code 32). A word preceded by a TAB (character code 9) or RETURN (character code 13) will have all its characters converted to lower case, but will not be properly capitalised. If the TAB and RETURN characters were valid word separators, the Titlecase function would be considerably more useful, especially for long entries in EditField and StaticText controls.

VB 6

StrConv function

StrConv(myString, vbProperCase)

Returns a string in which all letter characters at the start of each word are converted to uppercase. Valid word separators (and their character codes) are:

  • NULL (0)
  • TAB (9)
  • LINEFEED (10)
  • VERTICAL TAB (11)
  • FORM FEED (12)
  • RETURN (13)
  • SPACE (32)

VB .NET

StrConv function

StrConv(myString, VbStrConv.ProperCase)

Returns a string in which all letter characters at the start of each word are converted to uppercase. Valid word separators (and their character codes) are:

  • NULL (0)
  • TAB (9)
  • LINEFEED (10)
  • VERTICAL TAB (11)
  • FORM FEED (12)
  • RETURN (13)
  • SPACE (32)

 

Case, convert to upper

Given a string myString, an uppercase conversion function returns a new string containing myString, with all alphabetical characters converted to uppercase. For example, given the string "The Rosetta Stone", it should return "THE ROSETTA STONE".

C#

ToUpper method

myString.ToUpper()

MFC's CString class (C++)

TODO: Fill this in.

Python

upper function

from string import upper

upper(myString)

Returns a string containing the contents of myString, with all lowercase characters converted to uppercase.

STL's string class (C++)

TODO: Fill this in.

RB

Uppercase function

Uppercase(myString)

Returns a string containing the contents of myString, with all lowercase characters converted to uppercase.

VB 6

UCase / UCase$ functions

UCase(myString)

UCase$(myString)

UCase returns a variant (string) containing the contents of myString, with all lowercase characters converted to uppercase.

UCase$ does the same thing as UCase, except that it returns a string value instead of a string value contained withina variant.

 

StrConv function

StrConv(myString, vbUpperCase)

StrConv is a general-purpose string conversion function that can be used to convert to uppercase, among other things. vbUpperCase is a pre-defined constant whose value is 1. But really, why use this function when you've got UCase or UCase$, which are more readable and take only one argument?

VB .NET

ToUpper method

myString.ToUpper()

 

UCase / UCase$ functions

UCase(myString)

UCase$(myString)

 

StrConv function

StrConv(myString, VbStrConv.UpperCase)

StrConv is a general-purpose string conversion function that can be used to convert to uppercase, among other things. VbStrConv.UpperCase is a pre-defined constant. But really, why use this function when you've got ToUpper, which is more readable, standard across all CLR langauges and take only one argument?

Comparison function

Given two strings string1 and string2, a string comparison function returns an integer value that indicates the relationship between the two strings as shown below:

relationship between
string1 and string2
value returned by function
string1 < string2 negative
string1 = string2 0
string1 > string2 positive

 

C#

Compare method

String.Compare(string1, string2[, ignoreCase[, culture]])

Compare is case-sensitive by default. Case-sensitivity can be set through the optional boolean parameter ignoreCase; set it to false for a case-insensitive comparison or true for a case-sensitive one.

The optional parameter culture (a CultureInfo object) is used when making string comparisons using non-English languages.

MFC's CString class (C++)

TODO: Fill this in.

Python

cmp(string1, string2)

cmp is case-sensitive. To make it case-insensitive, use one of the case conversion functions -- either upper or lower will do -- to convert both string1 and string2. Here's how you encapsulate this as a function:

def noCaseCmp(string1, string2):
    from string import upper
    return cmp(upper(string1), upper(string2))

STL's string class (C++)

TODO: Fill this in.

RB

StrComp function

StrComp(string1 , string2, mode)

Case-sensitivity can be set through the integer parameter mode; set it to 0 for a case-sensitive comparison or 1 for a case-insensitive one.

VB

StrComp function

StrComp(string1, string2[, mode])

Case-sensitivity can be set through the integer parameter mode; set it to 0 (or the built-in contsant vbBinaryCompare) for a case-sensitive comparison or 1 (or the built-in constant vbTextCompare) for a case-insensitive one.

VB .NET

Compare method

String.Compare(string1, string2[, ignoreCase[, culture]])

Compare is case-sensitive. For a case-insensitive comparison, set the optional parameter ignoreCase (a boolean) to false.

The optional parameter culture (a CultureInfo object) is used when making string comparisons using non-English languages.

 

StrComp function

StrComp(string1, string2[, mode])

Case-sensitivity can be set through the parameter mode; set it to CompareMethod.Binary for a case-sensitive comparison orCompareMethod.Text for a case-insensitive one.

 

Concatenation

Given two or more strings, a concatenation function returns a new string made up of the given strings joined together in the order in which they were given. For example, given the strings "The", "Rosetta" and "Stone" in that order, it should return "TheRosettaStone".

C#

+ operator

string1 + string2

 

Concat function

String.Concat(string1, string2[, string3[, string4]])

 

+= operator

string1 += string2

Appends the contents of string2 to string1.

MFC's CString class (C++)

+ operator

string1 + string2

 

+= operator

string1 += string2

Appends the contents of string2 to string1.

 

Python

+ operator

string1 + string2

 

+= operator

string1 += string2

Appends the contents of string2 to string1.

 

STL's string class (C++)

+ operator

string1 + string2

 

+= operator

string1 += string2

Appends the contents of string2 to string1.

 

RB

+ operator

string1 + string2

 

VB 6

& and + operators

string1 & string2

string1 + string2

Although both & and + are string concatenation operators, & is the preferred one since there's no ambiguity, especially in the case of attempting to concatenate two string values contained in variants.

VB .NET

+ operator

string1 + string2

 

Concat function

String.Concat(string1, string2[, string3[, string4]])

 

 

Delete a substring from a string

Given a string myString, a start position start, and a length length, a "delete substring from a string" function returns a new string contaning myString with the substring of length length starting at position start removed. For example, given the string "The Rosetta Stone", a starting position of 4 (assuming the first position is 0) and a length of 7, it returns the string "The Stone".

C#

Remove method

myString.Remove(start, length)

 

 

Python

TODO: Fill this in.

STL's string class (C++)

myString.erase(start, length)

Removes length characters from myString, starting at position start.

RB

TODO: Fill this in.

VB 6

 

VB .NET

Remove method

myString.Remove(start, length)

 

Fields, nth in string

C#  
MFC's CString class (C++) TODO: Fill this in.
Python TODO: Fill this in.

STL's string class (C++)

TODO: Fill this in.
RB

NthField(Expression as String, separator as String, field as Integer)

Returns a string. This string is the nth substring (where n is field number) of string separated by separator character. The first field is numbered 1, not 0.

This is better illustrated by an example…

NthField ("First*Second*Third", "*", 2) would return "Second".

This function, and its partner, CountFields, probably have dozens of data-juggling uses.

VB

No equivalent.

Here's how you recreate RB's NthField method in VB6. This function uses the Split function, which is new to VB6.

Private Function NthField(expression as String, separator as String, fieldNum as Long) As String

Dim fields() as String

fields = Split(expression, separator, , vbTextCompare)
NthField - fields(fieldNum - 1)

End Function

 

Fields, number of

C#

 

MFC's CString class (C++)

TODO: Fill this in.

Python

TODO: Fill this in.

STL's string class (C++)

TODO: Fill this in.

RB

CountFields(expression as String, separator as Integer)

Returns an integer denoting the number of fields in a string, which are separated by separator character.

This is better illustrated by an example…

CountFields ("First*Second*Third") would return 3.

This function, and its partner, NthField, probably have dozens of data-juggling uses.

VB

Here's how you recreate RB's CountFields method in VB6. This function uses the Split function, which is new to VB6.

Function CountFields(expression as String, separator as String) as Long

Dim fields() as String

fields = Split(Expression, Separator, , vbTextCompare)
CountFields = UBound(fields) + 1

End Function

 

Find first occurrence

Given a string myString and a string searchString to search for within myString, a "find first occurrence" function returns the character position of the first occurence of the searchString within the myString if it can be found, or some sentinel value (usually -1) if it can't. For example, given the string "The Rosetta Stone" and the search string "Rosetta", it should return the value 4 (assuming the first character position is considered to be 0. This is the case for all Rosetta Stone languages except VB 6, where the first character position in considered to be 1. For VB 6, the function should return 5).

C#

IndexOf method

myString.IndexOf(searchSubstring)

 

MFC's CString class (C++) TODO: Fill this in.
Python

find function

from string import find

find(myString, searchSubstring[, start[, end]])

Returns an integer containing the lowest index in myString where searchSubstring can be found, or -1 if searchSubstring is not contained within myString.

If start is provided, the search for searchSubstring is limited to the slice beginning with start. If end is provided, the search for searchSubstring is limited to the slice ending with end.

 

index function

from string import index

index(myString, searchSubstring[, start[, end]])

Like find(), except that it raises a ValueError if searchSubstring is not contained within myString.

 

rfind function

from string import rfind

rfind(myString, searchSubstring[, start[, end]])

Like find(), except that it finds the highest index (that is, it starts search from the rightmost end of myString).

 

rindex function

from string import rindex

rindex(myString, searchSubstring[, start[, end]])

Like rfind(), except that it raises a ValueError if searchSubstring is not contained within myString.

STL's string class (C++)

find method

myString.find(searchSubstring[, start])

Searches myString for the first occurrence of searchSubstring (which can either be a string or char) starting at start (if start is omitted, the search starts at the beginning of the string). If searchSubstring is found, it returns the position of the first character in myString where the match occurs. If not, it returns the value string::npos.

 

rfind method

myString.rfind(searchSubstring[, start])

Like find(), except the search starts at the end of myString and proceeds from right to left.

RB

InStr function

InStr([start as Integer], phrase as String, searchString as String)

 

InStrB function

InStrB([start as Integer], phrase as String, searchString as String)

Instr returns an integer specifying the character position of the first occurrence of searchstring in string. The first character position of a string is 1, not 0. InStr returns 0 if string or searchstring is zero-length, searchstring is not found or start is past any occurrence of searchstring. This function is case-insensitive.

VB 6

InStr function

InStr([start as number], phrase as String, searchstring as String [, compare as integer])

Instr returns a variant of subtype long specifying the character position of the first occurrence of searchstring in string. The first character position of a string is 1, not 0. InStr returns 0 if string is zero-length, searchstring is not found or start is past any occurrence of searchstring. InStr returns Null if either string or searchstring are Null. InStr returns start if searchstring is zero-length.

start is an optional argument that specifies the character position where searching for substring in string should begin. The default is 1, referring to the first character in string.

compare is an optional argument that specifies the type of string comparison used. The default value of 0 (or the built-in constant vbBinaryCompare) means that the comparison is binary — that is, case-sensitive. A value of 1 (or the built-in constant vbTextCompare) means that the comparison is textual and hence case-insensitive. A value of 2 (or the built-in constant vbDatabaseCompare) is used for VBA in Microsoft Access only (see the VB documentation). If compare is omitted, the Option Compare setting determines the comparison type.

 

InStrB function

InStrB([start as number], phrase as String, searchstring as String [, compare as integer])

InStrB works similarly to InStr, except that it returns byte position instead of character positition.

 

InStrRev function

InStrRev(phrase as String, searchstring as String[, start as Integer[, compare as Integer]])

InStrRev is similar to InStr, except that its search is in the reverse direction (hence the name). InStrRev’s search starts with the last character and proceeds towards the first. There are two other differences:

  • The order of InStrRev’s optional parameters differs from that of InStr.
  • InStrRev returns a long while InStr returns a variant of subtype long.

VB and VBA in a Nutshell has this to say about InStrRev: "The usefulness of a function that looks backward through a string for the occurrence of another string isn’t immediately apparent. (p. 385)". However, it was apparent to Michael Loader, who reminded me vial e-mail that InStrRev can be used to find the last directory slash in a file path or last delimiter in a delimited string. VB6 has introduced some new string functions that can also be used for similar purposes.

VB .NET

IndexOf method

myString.IndexOf(searchSubstring)

 

InStr function

InStr([start as number], phrase as String, searchstring as String [, compare as integer])

 

InStrRev function

InStrRev(phrase as String, searchstring as String[, start as Integer[, compare as Integer]])

InStrRev is similar to InStr, except that its search is in the reverse direction (hence the name). InStrRev’s search starts with the last character and proceeds towards the first. There are two other differences:

  • The order of InStrRev’s optional parameters differs from that of InStr.
  • InStrRev returns a long while InStr returns a variant of subtype long.

VB and VBA in a Nutshell has this to say about InStrRev: "The usefulness of a function that looks backward through a string for the occurrence of another string isn’t immediately apparent. (p. 385)". However, it was apparent to Michael Loader, who reminded me vial e-mail that InStrRev can be used to find the last directory slash in a file path or last delimiter in a delimited string. VB6 has introduced some new string functions that can also be used for similar purposes.

 

Insert

Given a string myString, a start position start and a string insertString to insert into myString, an insert function returns a new string containing myString with insertString inserted into it at position start. For example, given the string "The Stone", a start position of 4 and an insertion string " Rosetta" (note the space at the start), it returns "The Rosetta Stone".

C#

Insert method

myString.Insert(start, insertString)

 

 

Python

TODO: Fill this in.

STL's string class (C++)

insert method

myString.insert(start, insertString)

RB

TODO: Fill this in.

VB 6

 

VB .NET

Insert method

myString.Insert(start, insertString)

Length of string

Given a string myString, a length function should return an integer value containing the number of characters in myString. For example, given the string "Rosetta Stone", the function should return 13.

C#

Length property

myString.Length

MFC's CString class (C++)

GetLength method

myString.GetLength()

Returns an integer containing the number of characters in myString.

Python

len function

len(myString)

Returns an integer containing the number of characters in myString.

STL's string class (C++)

size method

myString.size()

 

length method

myString.length()

Both methods return a value of type string::size_type containing the number of characters in myString.

RB

len function

len(myString)

Returns an integer containing the number of characters in myString.

 

lenB function

lenB(myString)

Returns an integer containing the number of bytes in myString.

VB 6

Len function

Len(myString)

Returns a long containing the number of characters in myString.

 

LenB function

LenB(myString)

Returns a long containing the number of bytes in myString.

VB .NET

Length property

myString.Length

 

Len function

Len(myString)

Returns an integer containing the number of characters in myString.

Repeated characters, create a string of

Given a character myCharacter and a number repetitions, a "repeating characters in a string" function return a string of length repetitions made up entirely of myCharacter characters. For example, given the character '*' and a value of 5, it would return the string "*****".

C#

Use custom function: RepeatedCharacters

C#'s string class doesn't have a method for creating a string of repeated characters. Use the function below (note that it's been declared as static -- you'll probably want to put it in a string utility class).

public static string RepeatedCharacters(char myCharacter, int repetitions)
{
    string result = "";
    return result.PadLeft(repetitions, myCharacter);
}

Java

TODO: Fill this in.

Python

TODO: Fill this in.

RB

Use custom function: RepeatedCharacters

RB doesn't have a built-in function for creating a string of repeated characters. Use the function below:

Public Function RepeatedCharacters(myCharacter as string, repetitions as integer) as String

   Dim result as string
   Dim I as integer

   
For I = 1 to repeats
       Result = result + character

    Next I

   
Return result

End Function

VB 6

String / String$ functions

String(value as number, character as String)

String$(value as number, character as String)

String returns a variant (string) made up of number occurences of character in a row.

String$ does the same thing as String, except that it returns a string value instead of a string value contained within a variant.

 

Space / Space$ functions

Space(value as number)

Space$(value as number)

Space returns a variant (string) made up of number occurences of space characters in a row.

Space$ does the same thing as Space, except that it returns a string value instead of a string value contained within a variant.

VB .NET

Use custom function: RepeatedCharacters

VB's string class doesn't have a method for creating a string of repeated characters, nor is there a built-in string function for creating a string made of repeated characters other than spaces. Use the function below.

Public Function RepeatedCharacters(myCharacter as Char, repetitions as Integer) as String

   Dim result as string

   result = ""
   RepeatedCharacters = result.PadLeft(repetitions, myCharacter);

End Function

 

Reverse string

Given a string myString, a reverse string function should return a new string made of up the characters in myString in reverse order. For example, given the string "Rosetta Stone", it should return "enotS attesoR".

C#

ReverseString method

C#'s string class doesn't have a method for reversing strings. Use the function below (note that it's been declared as static -- you'll probably want to put it in a string utility class).

public static string ReverseString(string theString)
{
    System.Text.StringBuilder theBuffer = new System.Text.StringBuilder();
    int i;

    for (i = (theString.Length - 1); i >= 0; i--)
    {
        theBuffer.Append(theString[i]);
    }

    return theBuffer.ToString();
}

Java

TODO: Fill this in.

Python

ReverseString function

Python doesn't have a built-in function for reversing strings, nor is there one in the string module. Use the function below.

def ReverseString(theString):

    result = ""

    for x in theString:
        result = x + theResult

    return result

RB

StrReverse function

RB doesn't have a built-in function for reversing strings. Use the function below.

Public Function ReverseString(s as String) as String

Dim i as integer
Dim result as string

if len(s) > 0 then

for i = len(s) downto 1

result = result + mid(s, i, 1)

next

end if

return result

End Function

VB 6

StrReverse function

StrReverse(myString)

StrReverse reverses the order of characters in a string.

VB .NET

StrReverse function

StrReverse(myString)

StrReverse reverses the order of characters in a string.

 

Search and replace

Given a string myString, a string searchString to search for within myString, and a replacement string replacementString, a search and replace function returns a new string made up of myString with replacementString substituting for all occurrences of searchString. For example, given the string "This is the Rosetta Stone", the search string "is" and the replacement string "at", it should return "That at the Rosetta Stone".

C#

Replace method

myString.Replace(searchString, replacementString)

 

Python

replace function

replace(myString, searchString, replacementString [, maxsplit])

RB

ReplaceAll function

ReplaceAll(myString, searchString, replacementString)

ReplaceAll is similar to Replace, except that all occurences of oldsubstring are replaced with newsubstring. If oldsubstring is the empty string (""), ReplaceAll returns an unchanged copy of string. If newsubstring is the empty string, ReplaceAll returns a copy of string with all occurrences of oldsubstring removed.

VB 6

Replace function

Replace(myString, searchString, replacementString)

If oldsubstring is the empty string (""), Replace returns an unchanged copy of myString. If newsubstring is the empty string, Replace returns a copy of string with one or more occurrences of oldsubstring removed.

VB .NET

Replace method

myString.Replace(searchString, replacementString)

 

Replace function

Replace(myString, searchString, replacementString)

 

Substring, general

There are two ways a substring function can work:

C#

Substring method

myString.Substring(start[, length])

MFC's CString class (C++)

TODO: Fill this in.

Python

Use slice notation

mystring[start:end]

Returns a string containing the slice defined by [start:end].

Remember that slice positions are defined not by character positions, but the "spaces between", with positon 0 being before the first character in the string, position 1 being after the first, position 2 after the second, and so on. For example, the following code:

z = "Hello World"
print z[2:4]

returns "ll" since the start position, 2, is between the e and l in "Hello" and the end position, 4, is between the l and o in "Hello".

STL's string class (C++)

substr method

myString.substr(start, length)

Returns a string created by copying length characters from myString starting at positon start. Both start and length are integers. Note than position 0 denotes the first character in the string.

If start + length is greater than myString.size(), the resulting string contains all the characters starting from position start to the end of the myString. The result is undefined if start is greater than myString.size().

RB

Mid function

Mid(myString, start [,length])

Mid returns a string containing length characters from the middle of string, starting with character start. The first character position of a string is 1, not 0. If length is not specified, then Mid returns the rightmost portion of the string, starting at character start (it essentially becomes the Right function).

 

MidB function

MidB(myString, start [,length])

Like Mid, except that the length parameter is expressed in bytes, not characters.

VB

Mid / Mid$ functions

Mid(myString, start [,length])

Mid$(myString, start [,length])

Returns a variant (string) containing length characters from the middle of string, starting with character start. The first character position of a string is 1, not 0. If length is not specified, then Mid returns the rightmost portion of the string, starting at character start (it essentially becomes the Right function).

Does the same thing as Mid, except that it returns a string value instead of a string value contained within a variant. Most texts on VB recommend the use of Mid$ over Mid, since it returns a string instead of a variant (theoretically yielding better performance and using less memory).

VB .NET

Substring method

myString.Substring(start[, length])

 

Mid function

Mid(myString, start [,length])

 

Substring, left

Given a string myString and a length length, a left substring function should return a new string made up of the leftmost length characters of myString. For example, given the string "The Rosetta Stone" and length of 3, it should return the string "The".

C#

 

MFC's CString class (C++)

TODO: Fill this in.

Python

myString[0:length]

Python doesn't have a built-in function for extracting the left portion of a string, but getting the [0:length] slice will give you the length leftmost characters of myString.

STL's string class (C++)

Use the substr() method:

myString.substr(0, length)

Returns a string containing the length leftmost characters of myString.

RB

Left(myString, length)

LeftB(myString, length)

Returns a string containing the length leftmost characters of myString.

VB

Left(myString, length)

Left$(myString, length)

Left returns a variant (string) containing the length leftmost characters of myString.

Left$ does the same thing as Left, except that it returns a string value instead of a string value contained within a variant. Most texts on VB recommend the use of Left$ over Left, since it returns a string instead of a variant (theoretically yielding better performance and using less memory).

 

Substring, right

Given a string myString and a length length, a right substring function should return a new string made up of the rightmost length characters of myString. For example, given the string "The Rosetta Stone" and length of 3, it should return the string "one".

C#

 

MFC's CString class (C++)

TODO: Fill this in.

Python

myString[len(myString) - length:]

Python doesn't have a built-in function for extracting the right portion of a string, but getting the [len(myString) - length:] slice will give you the length rightmost characters of myString.

STL's string class (C++)

Use the substr() and size() methods:

myString.substr(mystring.size() - length, length)

Returns a string containing the length rightnost characters of myString.

RB

Right(myString, length)

RightB(myString, length)

Returns a string containing the length rightmost characters of myString.

VB

Right(myString, length)

Right$(myString, length)

Right returns a variant (string) containing the length rightmost characters of myString.

Right$ does the same thing as Right, except that it returns a string value instead of a string value contained withina variant. Most texts on VB recommend the use of Right$ over Right, since it returns a string instead of a variant (theoretically yielding better performance and using less memory).

Whitespace characters, remove leading

Given a string myString, a "remove leading whitespace characters" function returns a new string made up of myString with any leading whitespace characters removed. For example, given the string "     The Rosetta Stone     ", it should return the string "The Rosetta Stone     ".

C#

TrimStart method

myString.TrimStart()

MFC's CString class (C++)

TODO: Fill this in.

Python

lstrip function

from string import lstrip

lstrip(myString)

Returns a string containing a copy of myString without any leading whitespace characters.

STL's string class (C++)

TODO: Fill this in.

RB

LTrim function

LTrim(myString)

Returns a string containing a copy of myString without any leading spaces.

VB 6

LTrim / LTrim$ function

LTrim(myString)

LTrim$(myString)

LTrim returns a variant (string) containing a copy of myString without any leading spaces.

LTrim$ does the same thing as LTrim, except that it returns a string value instead of a string value contained within a variant.

VB .NET

TrimStart method

myString.TrimStart()

 

LTrim function

LTrim(myString)

LTrim returns a variant string containing a copy of myString without any leading spaces.

 

Whitespace characters, remove leading and trailing

Given a string myString, a "remove leading and trailing whitespace characters" function returns a new string made up of myString with any leading and trailing whitespace characters removed. For example, given the string "     The Rosetta Stone     ", it should return the string "The Rosetta Stone".

C#

Trim method

myString.Trim()

MFC's CString class (C++)

TODO: Fill this in.

Python

strip function

from string import strip

strip(myString)

Returns a string containing a copy of myString without any leading or trailing whitespace characters.

STL's string class (C++)

TODO: Fill this in.

RB

Trim function

Trim(myString)

Returns a string containing a copy of myString without any leading or trailing spaces.

VB 6

Trim / Trim$ methods

Trim(myString)

Trim$(myString)

Trim returns a variant (string) containing a copy of myString without any leading or trailing spaces.

Trim$ does the same thing as Trim, except that it returns a string value instead of a string value contained within a variant.

VB .NET

Trim method

myString.Trim()

 

Trim function

Trim(myString)

 

 

Whitespace characters, remove trailing

Given a string myString, a "remove trailing whitespace characters" function returns a new string made up of myString with any trailing whitespace characters removed. For example, given the string "     The Rosetta Stone     ", it should return the string "     The Rosetta Stone".

C#

TrimEnd method

myString.TrimEnd()

MFC's CString class (C++)

TODO: Fill this in.

Python

rstrip function

from string import rstrip

rstrip(myString)

Returns a string containing a copy of myString without any trailing whitespace characters.

STL's string class (C++)

TODO: Fill this in.

RB

RTrim function

RTrim(myString)

Returns string containing a copy of myString without any trailing spaces.

VB 6

RTrim / RTrim$ functions

RTrim(myString)

RTrim$(myString)

RTrim returns a variant (string) containing a copy of myString without any trailing spaces.

RTrim$ does the same thing as RTrim, except that it returns a string value instead of a string value contained within a variant.

VB .NET

TrimEnd method

myString.TrimEnd()

 

RTrim function

RTrim(myString)

RTrim returns a string containing a copy of myString without any trailing spaces.