qb45advr.hlp (Topic list)
Selected String Manipulation Example Programs
  Selected Programs   String Manipulation   Contents   Index
──────────────────────────────────────────────────────────────────────────────
String Manipulation Programs
 
The following programming examples demonstrate various ways to manipulate
sequences of ASCII characters known as strings.
 
Example 1
 
One of the most common string-processing tasks is searching for a string
inside another string. The INSTR function tells you whether or not
string2 is contained in string1 by returning the position of the first
character in string1 (if any) where the match begins. If no match is
found (that is, string2 is not a substring of string1, INSTR returns
the value 0.
 
The following programming example demonstrates this:
 
  String1$ = "A line of text with 37 letters in it."
  String2$ = "letters"
  PRINT "         1         2         3         4"
  PRINT "1234567890123456789012345678901234567890"
  PRINT String1$
  PRINT String2$
  PRINT INSTR(String1$, String2$)
 
Sample output
 
         1         2         3         4
1234567890123456789012345678901234567890
A line of text with 37 letters in it.
letters
 24
 
Example 2
 
The INSTR(position, string1, string2) variation is useful for finding
every occurrence of string2 in string1, instead of just the first
occurrence of string2 in string1, as shown in the next example:
 
  String1$ = "the quick basic jumped over the broken saxophone."
  String2$ = "the"
  PRINT String1$
  Start = 1
  NumMatches = 0
  DO
    Match = INSTR(Start, String1$, String2$)
    IF Match > 0 THEN
      PRINT TAB(Match); String2$
      Start = Match + 1
      NumMatches = NumMatches + 1
    END IF
  LOOP WHILE MATCH
  PRINT "Number of matches ="; NumMatches
 
Sample output
 
the quick basic jumped over the broken saxophone.
the
                            the
Number of matches = 2