String
Last updated at 9:57 am UTC on 19 July 2022
ArrayedCollection subclass: #String
instanceVariableNames: ''
classVariableNames: 'AsciiOrder CSMacroCharacters CaseInsensitiveOrder CaseSensitiveOrder CrLfExchangeTable FormatCharacterSet HtmlEntities LowercasingTable Tokenish UppercasingTable'
poolDictionaries: ''
category: 'Collections-Strings'
String subclasses {ByteString . WideString . Symbol}
String class comment
(Squeak 5.0)
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 match:
String contractTo:
String also inherits many useful methods from its hierarchy, such as
SequenceableCollection ,
So
SequenceableCollection copyReplaceAll:with:
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.
Subclasses are:
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)
- accessing
- at:, at: put:
- findString:, findAnySubStr: startingAt:, findCloseParenthesisFor:
- tabDelimitedFieldsDo;, indexOf:
- comparing
- <, >, =, ≤, ≥, compare:
- beginsWith:, endsWith:, endsWithAnyOf:
- converting
- asMorph, asDate, asHtml, asHex, asParagraph, asUpperCase
- subStrings, romanNumber, capitalized, asUrl
- printing
- encodeDoublingQuoteOn:, printOn:, storeOn:
Some common class methods
- instance creation
- cr, crlf, tab, lf (create strings of non-enterable characters)
- fromString:, readFrom:
- initialize
Examples
"'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: ',']
Generated documentation?
The objects of the class String are collections of characters which have specific methods for dealing with them.
(Squeak 3.3a-4664)
String selectors size is 173
String allSelectors size is 730
ProtoObject #()
Object #()
Collection #()
Text #()
ArrayedCollection #()
String #()
Symbol #()
Hints and tips
Simple string pattern matching! Do print it on each line,
see method
String>>match: for more details."
'abc*' match: 'abcdef'
'ab#de#' match: 'abcdef'
'*def' match: 'abcdef'
See also
Reading a file into a string
Some String messages of interest
Selectors of String and Text