22
Manipulating Strings String Functions

Manipulating Strings String Functions. VB provides a large number of functions that facilitate working with strings. These are found in Microsoft.VisualBasic.Strings

Embed Size (px)

Citation preview

Manipulating Strings

String Functions

String Functions

VB provides a large number of functions that facilitate working with strings.

These are found in

Microsoft.VisualBasic.Strings

For convenience they can be grouped according to the kind of task they’re designed to carry out.

Inspection Functions

Inspection functions take a string (or strings) as argument(s) and return information about them in numeric form.

Inspection Functions

Function Name Return type Description

Asc (String1) Integer Returns an Integer value representing the character code corresponding to a character.

Len (String1) Integer Returns an Integer, the number of characters in String1.

InStr ([Start (Integer),] String1, String2)

Integer Returns an Integer specifying the position of the first occurrence of String2 in String1, starting at Start if the argument is specified, otherwise at the beginning of String1.

InStrRev (String1, String2, [Start (Integer]))

Integer Returns an Integer specifying the position of the first occurrence of String2 in String1, from the right of String1 (or from Start if the argument is specified.

StrComp (String1, String2)

Integer Returns an Integer indicating the comparison of String1 and String2, namely -1, 0 or +1 depending if String1 sorts ahead of, is the same as, or sorts later than String2.

Inspection Functions

Len(“some text”)

InStr(“some text”, “e”)

InStr(“some text”, “a”)

InStr(5, “some text”, “e”)

InStrRev(“some text”, “e”)

InStrRev(“some text”, “e”, 5)

StrComp(“text”, “test”)

Inspection Functions

Len(“some text”) 9

InStr(“some text”, “e”)

InStr(“some text”, “a”)

InStr(5, “some text”, “e”)

InStrRev(“some text”, “e”)

InStrRev(“some text”, “e”, 5)

StrComp(“text”, “test”)

Inspection Functions

Len(“some text”) 9

InStr(“some text”, “e”) 4

InStr(“some text”, “a”)

InStr(5, “some text”, “e”)

InStrRev(“some text”, “e”)

InStrRev(“some text”, “e”, 5)

StrComp(“text”, “test”)

Inspection Functions

Len(“some text”) 9

InStr(“some text”, “e”) 4

InStr(“some text”, “a”) 0

InStr(5, “some text”, “e”)

InStrRev(“some text”, “e”)

InStrRev(“some text”, “e”, 5)

StrComp(“text”, “test”)

Inspection Functions

Len(“some text”) 9

InStr(“some text”, “e”) 4

InStr(“some text”, “a”) 0

InStr(5, “some text”, “e”) 7

InStrRev(“some text”, “e”)

InStrRev(“some text”, “e”, 5)

StrComp(“text”, “test”)

Inspection Functions

Len(“some text”) 9

InStr(“some text”, “e”) 4

InStr(“some text”, “a”) 0

InStr(5, “some text”, “e”) 7

InStrRev(“some text”, “e”) 7

InStrRev(“some text”, “e”, 5)

StrComp(“text”, “test”)

Inspection Functions

Len(“some text”) 9

InStr(“some text”, “e”) 4

InStr(“some text”, “a”) 0

InStr(5, “some text”, “e”) 7

InStrRev(“some text”, “e”) 7

InStrRev(“some text”, “e”, 5) 4

StrComp(“text”, “test”)

Inspection Functions

Len(“some text”) 9

InStr(“some text”, “e”) 4

InStr(“some text”, “a”) 0

InStr(5, “some text”, “e”) 7

InStrRev(“some text”, “e”) 7

InStrRev(“some text”, “e”, 5) 4

StrComp(“text”, “test”) 1

Case Conversion Functions

Function Name Return type Description

LCase (String1) String Returns String1 converted to lower case

UCase (String1) String Returns String1 converted to UPPER case

These are fairly obvious.

SubString Functions

There are several functions provided to extract part of string.

Function Name Return type Description

Left (String1, Length ) String Returns a string containing the specified number of characters from the left of String1.

Right (String1, Length ) String Returns a string containing the specified number of characters from the right of String1.

Mid (String1, Start, [Length])

String Returns all (or Length if it is specified) characters from String1 starting at position Start.

Formatting Functions

Function Name Return type Description

LSet (String1, Integer) String Returns a left-aligned string containing String1 adjusted to the specified length.

LTrim (String1) String Returns a string with blanks removed from the left of String1

RSet (String1, Integer) String Returns a right-aligned string containing String1 adjusted to the specified length.

RTrim (String1) String Returns a string with blanks removed from the right of String1

String Creation Functions

Function Name Return type

Description

Chr (Integer) Char Returns the character associated with the specified character code.

Space (Integer) String Returns a string composed of blanks, as many as specified by Integer.

StrReverse (String1) String Returns a string composed of the characters from String1 but in reverse order.

Join (SourceArray() [,Delimiter])

String Returns a string created by joining a number of substrings contained in an array. Is a character to be used to separate the strings.

Replace (String1, String2, String3,[Start], [Count])

String Returns a string with String2 replaced by String3, where ever it is found in String1, beginning at position Start, or replaces it Count times from position Start.

String Creation Functions

Replace (“some people”, “o”, “a”)

String Creation Functions

Replace (“some people”, “o”, “a”)

“same peaple”

String Creation Functions

Replace (“some people”, “o”, “a”)

“same peaple”

Replace (“some people”, “o”, “a”, , 1)

String Creation Functions

Replace (“some people”, “o”, “a”)

“same peaple”

Replace (“some people”, “o”, “a”, , 1)

“same people”

String Creation Functions

Replace (“some people”, “o”, “a”)

“same peaple”

Replace (“some people”, “o”, “a”, , 1)

“same people”

Replace (“some people”, “o”, “a”, 5)

String Creation Functions

Replace (“some people”, “o”, “a”)

“same peaple”

Replace (“some people”, “o”, “a”, , 1)

“same people”

Replace (“some people”, “o”, “a”, 5)

“ peaple”

Notice that Start indicates the starting character for the Result string.