6.2.4 String methods

Strings have many methods for performing simple string manipulations. Here we list their names using the foreach command, which will be introduced in the next chapter:

pyxplot> foreach m in "".methods() { print m ; }
append
beginsWith
class
contents
data
endsWith
find
findAll
isalnum
isalpha
isdigit
len
lower
lstrip
methods
rstrip
split
splitOn
str
strip
type
upper

Full documentation of them can be found in Section 13.15. As in Python, the strip() method removes whitespace characters from the beginning and end of strings, and the split() method splits a string up into whitespace-separated words. The splitOn(x) method splits a string on all occurrences of the sub-string x. The following examples demonstrate the use of some of them:

pyxplot> x="It was the best of times, it was the worst of times"
pyxplot> print x.len()
51
pyxplot> print x.split()[0:5]
["It", "was", "the", "best", "of"]
pyxplot> print x.splitOn(",")
["It was the best of times", " it was the worst of times"]
pyxplot> print x.find("worst")
37
pyxplot> print x[0:24]
It was the best of times
pyxplot> print x[-25:]
it was the worst of times
pyxplot> print x.upper()
IT WAS THE BEST OF TIMES, IT WAS THE WORST OF TIMES

pyxplot> x="    BEAUTIFUL new railway bridge of the Silvery Tay,"
pyxplot> print x.lstrip()
BEAUTIFUL new railway bridge of the Silvery Tay,