Manipulating Strings
What is a string? Data composed of text characters. The data is stored in consecutive bytes of memory. Each byte stores the ASCII code for a single character.
The ASCII Character Set Hex ABCDEF Binary BSHTLF CR ESC !"#$%&'()*+,-./ :;<=>? PQRSTUVWXYZ[\]^_ `abcdefghijklmno pqrstuvwxyz{|}~DEL
Storing a String When a String type variable is declared in VB6, 256 consecutive bytes of memory are reserved. The ASCII code for the fist character is stored in the first byte reserved. The code for the next character in the string is stored in the next byte.
Storing a String If the statement Dim name As String reserves 256 bytes starting at 1000(Hex), then Name = “Lew” Places the ASCII code for ‘L’ in 1000, and the code for ‘e’ in 1001…
Storing a String The ASCII table provides the codes: L – e – w –
Storing a String Memory locationContent Random junk ″ …
How are strings manipulated? Program design requirements can pose a number of problems that can be solved by string manipulation. Chapter 5 of the “Programming for Literacy” illustrates several examples.
Inspection Functions Function NameReturn typeDescription Len (String1)LongReturns a Long, the number of characters in String1 InStr ([Start (Integer),] String1, String2) LongReturns a Long 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])) LongReturns a Long specifying the position of the first occurrence of String2 in String1, from the end of String1 (or from Start if the argument is specified StrComp (String1, String2)IntegerReturns an integer indicating the comparison of String1 and String2, namely -1, 0 or +1 depending if String1 is less than, equal to, or greater than String2
Conversion Functions Function NameReturn typeDescription Asc (String)IntegerReturns an Integer, being the character code for the first character in the string Chr (Long)StringReturns the character corresponding to the specified code Str (Long)StringReturns a string representation of the number LCase (String1)StringReturns String1 converted to lower case UCase (String1)StringReturns String1 converted to UPPER case
SubString Functions Function NameReturn typeDescription Left (String1, Integer)StringReturns a string containing the specified number of characters from the left of String1 Ltrim (String1)StringReturns a string with blanks removed from the left of String1 Right (String1, Integer)StringReturns a string containing the specified number of characters from the right of String1 Rtrim (String1)StringReturns a string with blanks removed from the right of String1 Mid (String1, Start (Long), [Length (Long)]) StringReturns all (or Length if it is specified) characters from String1 starting at position Start
String Creation Functions Function NameReturn typeDescription Space (Long)StringReturns a string composed of blanks, as many as specified by Long String (Long, String1)StringReturns a string composed of the first character of String1, repeated as many times as specified by Long StrReverse (String1)StringReturns a string composed of the characters from String1 but in reverse order Replace (String1, String2, String3, [Start (Long)], [Count]) StringReturns 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