Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Built-in functions

The everyday operations on text are built into the language: no #import is needed for any of them, and they all work on ustring.

'' The string functions built into the language. Positions count
'' from 1, and a search that finds nothing returns 0.
dim as ustring s = "The quick brown fox"

print "["; left( s, 3 ); "] ["; right( s, 3 ); "]"
print "["; mid( s, 5, 5 ); "] ["; mid( s, 11 ); "]"

print "brown is at"; instr( s, "brown" )
print "from 14 on, o is at"; instr( 14, s, "o" )
print "last o is at"; instrrev( s, "o" )
print "zebra is at"; instr( s, "zebra" )

print ucase( s )
print lcase( s )

dim as ustring padded = "   spaced out   "
print "["; trim( padded ); "]"
print "["; ltrim( padded ); "] ["; rtrim( padded ); "]"
print "["; trim( "--core--", any "-" ); "]"

print "["; space( 4 ); "] ["; string( 6, "=" ); "]"
print "chr( 65 ) is " & chr( 65 ) & ", asc( ""B"" ) is " & asc( "B" )
print "the third character's code is " & asc( s, 3 )

'' mid on the left of = overwrites characters in place.
mid( s, 5, 5 ) = "QUICK"
print s

'' str and val convert between numbers and text.
dim as ustring n = str( 6 * 7 )
print "["; n; "]"; val( "2.5" ) * 2

Output

[The] [fox]
[quick] [brown fox]
brown is at 11
from 14 on, o is at 18
last o is at 18
zebra is at 0
THE QUICK BROWN FOX
the quick brown fox
[spaced out]
[spaced out   ] [   spaced out]
[core]
[    ] [======]
chr( 65 ) is A, asc( "B" ) is 66
the third character's code is 101
The QUICK brown fox
[42] 5

Two conventions run through every one of them:

  • Positions start at 1. mid( s, 5, 5 ) is five characters starting at the fifth.
  • Not found is 0. instr returns 0 when the text is absent, which is never a valid position, so if instr( s, x ) > 0 is the test.
FunctionGives
left( s, n ), right( s, n )the first or last n characters
mid( s, start, n )n characters from start; leave out n for the rest
instr( s, t ), instr( start, s, t )where t first appears, searching from start
instrrev( s, t )where t last appears
ucase( s ), lcase( s )the text in upper or lower case
trim, ltrim, rtrimthe text without surrounding spaces; any "chars" strips those characters instead
space( n ), string( n, c )n spaces, or n copies of a character
chr( n ), asc( s, i )a character from its code, and the code of the character at i
str( n ), val( s )a number as text, and text as a number

None of these change their argument: each returns a new string. The exception is mid written on the left of =, the mid statement, which overwrites characters inside an existing string. It never changes the string's length, so it is also how Building strings fills a buffer without reallocating.

Converting between numbers and text in more depth, including what val does with text that is not a number, is in Conversion.

Activities

  1. Use instr in a do loop to count how many times "o" appears in s, passing the position after each match as the start of the next search.
  2. Print s with its first letter in lower case and the rest in upper case, using left, mid, lcase and ucase.