Last updated at 1:48 pm UTC on 19 November 2019
String class comment
A String is an indexed collection of Characters. Class String provides the abstract super class for ByteString (that represents an array of 8-bit Characters) and WideString (that represents an array of 32-bit characters). In the similar manner of LargeInteger and SmallInteger, those subclasses are chosen accordingly for a string; namely as long as the system can figure out so, the String is used to represent the given string.
Strings support a vast array of useful methods, which can best be learned by browsing and trying out examples as you find them in the code.
Here are a few useful methods to look at...
String also inherits many useful methods from its hierarchy, such as
is used to do String substitution.
Strings are kept as Unicode code points arrays, and converted to encoded forms when entering/exiting the system.
Pharo/Squeak Strings keep data in UTF32 encoding form, where 1 codepoint = 1 code unit, dynamically switched between Latin1 (ByteStrings) and UTF32 (WideStrings) encoding schemes as needed.
The distinction between ByteString and WideString is a mostly invisible implementation detail.
See String equivalence (Unicode)
Some common instance methods (too many to list but here are a few)
- at:, at: put:
- findString:, findAnySubStr: startingAt:, findCloseParenthesisFor:
- tabDelimitedFieldsDo;, indexOf:
- <, >, =, ≤, ≥, compare:
- beginsWith:, endsWith:, endsWithAnyOf:
- asMorph, asDate, asHtml, asHex, asParagraph, asUpperCase
- subStrings, romanNumber, capitalized, asUrl
- encodeDoublingQuoteOn:, printOn:, storeOn:
Some common class methods
- instance creation
- cr, crlf, tab, lf (create strings of non-enterable characters)
- fromString:, readFrom:
"'abcd' capitalized" -> 'Abcd'
"'abcd' endsWithAnyOf: #('d', 'D')" -> true
"('abcd' at:2)." -> $b
"'abcd' at: 2 put: $3" -> $3 (returns the put: argument)
"c _ 'abcd'. c byteAt:2 put:35. c." -> 'a#cd'
"('a,b c,d' findTokens: (String with: Character cr))
collect: [:aLine | aLine findTokens: ',']
The objects of the class String are collections of characters which have specific methods for dealing with them.
String selectors size is 173
String allSelectors size is 730
Hints and tips
Simple string pattern matching! Do print it on each line,
String>>match: for more details."
'abc*' match: 'abcdef'
'ab#de#' match: 'abcdef'
'*def' match: 'abcdef'
Reading a file into a string
Some String messages of interest