Squeak Class Documentation category index | class index  
 
String
  category: Collections-Text
  superclass: ArrayedCollection
  subclasses: Symbol

A String is an indexed collection of Characters, compactly encoded as 8-bit bytes.

String 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 ,
SequenceableCollection copyReplaceAll:with:

instance methods
  Camp Smalltalk
  sunitAsSymbol
sunitMatch:
sunitSubStrings

  Celeste
  withCRs

  accessing
  at:
at:put:
byteAt:
byteAt:put:
byteSize
endsWithDigit
findAnySubStr:startingAt:
findBetweenSubStrs:
findCloseParenthesisFor:
findDelimiters:startingAt:
findString:
findString:startingAt:
findString:startingAt:caseSensitive:
findTokens:
findTokens:includes:
findTokens:keep:
findWordStart:startingAt:
includesSubString:
includesSubstring:caseSensitive:
indexOf:
indexOf:startingAt:
indexOf:startingAt:ifAbsent:
indexOfAnyOf:
indexOfAnyOf:ifAbsent:
indexOfAnyOf:startingAt:
indexOfAnyOf:startingAt:ifAbsent:
indexOfSubCollection:startingAt:ifAbsent:
lastIndexOfPKSignature:
lineCorrespondingToIndex:
lineCount
lineNumber:
linesDo:
skipAnySubStr:startingAt:
skipDelimiters:startingAt:
startsWithDigit

  arithmetic
  *
+
-
/
//
\\

  comparing
  <
<=
=
>
>=
alike:
beginsWith:
caseInsensitiveLessOrEqual:
caseSensitiveLessOrEqual:
charactersExactlyMatching:
compare:
crc16
endsWith:
endsWithAnyOf:
hash
hashMappedBy:
howManyMatch:
match:
sameAs:
startingAt:match:startingAt:

  converting
  adaptToCollection:andSend:
adaptToNumber:andSend:
adaptToPoint:andSend:
adaptToString:andSend:
asCharacter
asDate
asDisplayText
asFileName
asFourCode
asHex
asHtml
asIRCLowercase
asIdentifier:
asInteger
asLegalSelector
asLowercase
asNumber
asPacked
asParagraph
asPostscript
asString
asStringOrText
asSymbol
asText
asTime
asUnHtml
asUppercase
asUrl
asUrlRelativeTo:
askIfAddStyle:req:
capitalized
compressWithTable:
contractTo:
correctAgainst:
correctAgainst:continuedFrom:
correctAgainstDictionary:continuedFrom:
encodeForHTTP
findSelector
initialIntegerOrNil
keywords
numericSuffix
romanNumber
sansPeriodSuffix
splitInteger
stemAndNumericSuffix
substrings
surroundedBySingleQuotes
translateFrom:to:table:
translateToLowercase
translateToUppercase
translateWith:
truncateTo:
truncateWithElipsisTo:
unparenthetically
unzipped
withBlanksCondensed
withBlanksTrimmed
withFirstCharacterDownshifted
withNoLineLongerThan:
withSeparatorsCompacted
withoutLeadingDigits
withoutTrailingBlanks
withoutTrailingDigits

  copying
  copyReplaceTokens:with:
deepCopy
padded:to:with:

  displaying
  displayAt:
displayOn:
displayOn:at:
displayProgressAt:from:to:during:
newTileMorphRepresentative

  encoding
  getInteger32:
primGetInteger32:
primPutInteger32:at:
putInteger32:at:

  filter streaming
  byteEncode:
putOn:

  internet
  decodeMimeHeader
decodeQuotedPrintable
isoToSqueak
replaceHtmlCharRefs
squeakToIso
unescapePercents
withInternetLineEndings
withSqueakLineEndings
withoutQuoting

  paragraph support
  indentationIfBlank:

  printing
  basicType
encodeDoublingQuoteOn:
isLiteral
printOn:
storeOn:
stringRepresentation

  private
  correctAgainstEnumerator:continuedFrom:
replaceFrom:to:with:startingAt:
stringhash

  system primitives
  compare:with:collated:
findSubstring:in:startingAt:matchTable:
numArgs

  testing
  hasContentsInExplorer
isAllDigits
isAllSeparators
isString
lastSpacePosition

  user interface
  asExplorerString
openInWorkspaceWithTitle:

class methods
  examples
  example
exampleFor:

  initialization
  initialize
initializeHtmlEntities

  instance creation
  cr
crlf
crlfcrlf
fromPacked:
fromString:
lf
readFrom:
tab

  internet
 

  plugin generation
  ccg:prolog:expr:index:
ccgDeclareCForVar:

  primitives
  findFirstInString:inSet:startingAt:
indexOfAscii:inString:startingAt:
stringHash:initialHash:
translate:from:to:table:

instance methods
  Camp Smalltalk top  
 

sunitAsSymbol


 

sunitMatch:


 

sunitSubStrings


  Celeste top  
 

withCRs

Return a copy of the receiver in which backslash (\) characters have been replaced with carriage returns.


  accessing top  
 

at:

Primitive. Answer the Character stored in the field of the receiver
indexed by the argument. Fail if the index argument is not an Integer or
is out of bounds. Essential. See Object documentation whatIsAPrimitive.


 

at:put:

Primitive. Store the Character in the field of the receiver indicated by
the index. Fail if the index is not an Integer or is out of bounds, or if
the argument is not a Character. Essential. See Object documentation
whatIsAPrimitive.


 

byteAt:


 

byteAt:put:


 

byteSize


 

endsWithDigit

Answer whether the receiver's final character represents a digit. 3/11/96 sw


 

findAnySubStr:startingAt:

Answer the index of the character within the receiver, starting at start, that begins a substring matching one of the delimiters. delimiters is an Array of Strings (Characters are permitted also). If the receiver does not contain any of the delimiters, answer size + 1.


 

findBetweenSubStrs:

Answer the collection of String tokens that result from parsing self. Tokens are separated by 'delimiters', which can be a collection of Strings, or a collection of Characters. Several delimiters in a row are considered as just one separation.


 

findCloseParenthesisFor:

assume (self at: startIndex) is $(. Find the matching $), allowing parentheses to nest.


 

findDelimiters:startingAt:

Answer the index of the character within the receiver, starting at start, that matches one of the delimiters. If the receiver does not contain any of the delimiters, answer size + 1.


 

findString:

Answer the index of subString within the receiver, starting at start. If
the receiver does not contain subString, answer 0.


 

findString:startingAt:

Answer the index of subString within the receiver, starting at start. If
the receiver does not contain subString, answer 0.


 

findString:startingAt:caseSensitive:

Answer the index in this String at which the substring key first occurs, at or beyond start. The match can be case-sensitive or not. If no match is found, zero will be returned.


 

findTokens:

Answer the collection of tokens that result from parsing self. Return strings between the delimiters. Any character in the Collection delimiters marks a border. Several delimiters in a row are considered as just one separation. Also, allow delimiters to be a single character.


 

findTokens:includes:

Divide self into pieces using delimiters. Return the piece that includes subString anywhere in it. Is case sensitive (say asLowercase to everything beforehand to make insensitive).


 

findTokens:keep:

Answer the collection of tokens that result from parsing self. The tokens are seperated by delimiters, any of a string of characters. If a delimiter is also in keepers, make a token for it. (Very useful for carriage return. A sole return ends a line, but is also saved as a token so you can see where the line breaks were.)


 

findWordStart:startingAt:

HyperCard style searching. Answer the index in self of the substring key, when that key is preceeded by a separator character. Must occur at or beyond start. The match is case-insensitive. If no match is found, zero will be returned.


 

includesSubString:


 

includesSubstring:caseSensitive:


 

indexOf:

Answer the index of the first occurence of anElement within the
receiver. If the receiver does not contain anElement, answer 0.


 

indexOf:startingAt:


 

indexOf:startingAt:ifAbsent:

Answer the index of the first occurence of anElement after start
within the receiver. If the receiver does not contain anElement,
answer the result of evaluating the argument, exceptionBlock.


 

indexOfAnyOf:

returns the index of the first character in the given set. Returns 0 if none are found


 

indexOfAnyOf:ifAbsent:

returns the index of the first character in the given set. Returns the evaluation of aBlock if none are found


 

indexOfAnyOf:startingAt:

returns the index of the first character in the given set, starting from start. Returns 0 if none are found


 

indexOfAnyOf:startingAt:ifAbsent:

returns the index of the first character in the given set, starting from start


 

indexOfSubCollection:startingAt:ifAbsent:

Answer the index of the receiver's first element, such that that element
equals the first element of sub, and the next elements equal
the rest of the elements of sub. Begin the search at element
start of the receiver. If no such match is found, answer the result of
evaluating argument, exceptionBlock.


 

lastIndexOfPKSignature:

Answer the last index in me where aSignature (4 bytes long) occurs, or 0 if not found


 

lineCorrespondingToIndex:

Answer a string containing the line at the given character position. 1/15/96 sw: Inefficient first stab at this


 

lineCount

Answer the number of lines represented by the receiver, where every cr adds one line. 5/10/96 sw


 

lineNumber:

Answer a string containing the characters in the given line number. 5/10/96 sw


 

linesDo:

execute aBlock with each line in this string. The terminating CR's are not included in what is passed to aBlock


 

skipAnySubStr:startingAt:

Answer the index of the last character within the receiver, starting at start, that does NOT match one of the delimiters. delimiters is a Array of substrings (Characters also allowed). If the receiver is all delimiters, answer size + 1.


 

skipDelimiters:startingAt:

Answer the index of the character within the receiver, starting at start, that does NOT match one of the delimiters. If the receiver does not contain any of the delimiters, answer size + 1. Assumes the delimiters to be a non-empty string.


 

startsWithDigit

Answer whether the receiver's first character represents a digit


  arithmetic top  
 

*


 

+


 

-


 

/


 

//


 

\\


  comparing top  
 

<

Answer whether the receiver sorts before aString.
The collation order is simple ascii (with case differences).


 

<=

Answer whether the receiver sorts before or equal to aString.
The collation order is simple ascii (with case differences).


 

=

Answer whether the receiver sorts equally as aString.
The collation order is simple ascii (with case differences).


 

>

Answer whether the receiver sorts after aString.
The collation order is simple ascii (with case differences).


 

>=

Answer whether the receiver sorts after or equal to aString.
The collation order is simple ascii (with case differences).


 

alike:

Answer some indication of how alike the receiver is to the argument, 0 is no match, twice aString size is best score. Case is ignored.


 

beginsWith:

Answer whether the receiver begins with the given prefix string.
The comparison is case-sensitive.


 

caseInsensitiveLessOrEqual:

Answer whether the receiver sorts before or equal to aString.
The collation order is case insensitive.


 

caseSensitiveLessOrEqual:

Answer whether the receiver sorts before or equal to aString.
The collation order is case sensitive.


 

charactersExactlyMatching:

Do a character-by-character comparison between the receiver and aString. Return the index of the final character that matched exactly.


 

compare:

Answer a comparison code telling how the receiver sorts relative to aString:
1 - before
2 - equal
3 - after.
The collation sequence is ascii with case differences ignored.
To get the effect of a <= b, but ignoring case, use (a compare: b) <= 2.


 

crc16

Compute a 16 bit cyclic redundancy check.


 

endsWith:

Answer whether the tail end of the receiver is the same as suffix.
The comparison is case-sensitive.


 

endsWithAnyOf:


 

hash

#hash is implemented, because #= is implemented


 

hashMappedBy:

My hash is independent of my oop.


 

howManyMatch:

Count the number of characters that match up in self and aString.


 

match:

Answer whether text matches the pattern in this string.
Matching ignores upper/lower case differences.
Where this string contains #, text may contain any character.
Where this string contains *, text may contain any sequence of characters.


 

sameAs:

Answer whether the receiver sorts equal to aString. The
collation sequence is ascii with case differences ignored.


 

startingAt:match:startingAt:

Answer whether text matches the pattern in this string.
Matching ignores upper/lower case differences.
Where this string contains #, text may contain any character.
Where this string contains *, text may contain any sequence of characters.


  converting top  
 

adaptToCollection:andSend:

If I am involved in arithmetic with a collection, convert me to a number.


 

adaptToNumber:andSend:

If I am involved in arithmetic with a number, convert me to a number.


 

adaptToPoint:andSend:

If I am involved in arithmetic with a point, convert me to a number.


 

adaptToString:andSend:

If I am involved in arithmetic with a string, convert us both to
numbers, and return the printString of the result.


 

asCharacter

Answer the receiver's first character, or '¥' if none. Idiosyncratic, provisional.


 

asDate

Many allowed forms, see Date.readFrom:


 

asDisplayText

Answer a DisplayText whose text string is the receiver.


 

asFileName

Answer a String made up from the receiver that is an acceptable file
name.


 

asFourCode


 

asHex


 

asHtml

Do the basic character conversion for HTML. Leave all original return
and tabs in place, so can conver back by simply removing bracked
things. 4/4/96 tk


 

asIRCLowercase

Answer a String made up from the receiver whose characters are all
lowercase, where 'lowercase' is by IRC's definition


 

asIdentifier:

Return a legal identifier, with first character in upper case if shouldBeCapitalized is true, else lower case. This will always return a legal identifier, even for an empty string


 

asInteger

Answer the Integer created by interpreting the receiver as the string representation of an integer. Answer nil if no digits, else find the first digit and then all consecutive digits after that


 

asLegalSelector


 

asLowercase

Answer a String made up from the receiver whose characters are all
lowercase.


 

asNumber

Answer the Number created by interpreting the receiver as the string
representation of a number.


 

asPacked

Convert to a longinteger that describes the string


 

asParagraph

Answer a Paragraph whose text string is the receiver.


 

asPostscript


 

asString

Answer this string.


 

asStringOrText

Answer this string.


 

asSymbol

Answer the unique Symbol whose characters are the characters of the
string.


 

asText

Answer a Text whose string is the receiver.


 

asTime

Many allowed forms, see Time.readFrom:


 

asUnHtml

Strip out all Html stuff (commands in angle brackets <>) and convert
the characters &<> back to their real value. Leave actual cr and tab as
they were in text.


 

asUppercase

Answer a String made up from the receiver whose characters are all
uppercase.


 

asUrl

convert to a Url


 

asUrlRelativeTo:


 

askIfAddStyle:req:


 

capitalized

Return a copy with the first letter capitalized


 

compressWithTable:

Return a string with all substrings that occur in tokens replaced
by a character with ascii code = 127 + token index.
This will work best if tokens are sorted by size.
Assumes this string contains no characters > 127, or that they
are intentionally there and will not interfere with this process.


 

contractTo:

return myself or a copy shortened by ellipsis to smallSize


 

correctAgainst:

Correct the receiver: assume it is a misspelled word and return the (maximum of five) nearest words in the wordList. Depends on the scoring scheme of alike:


 

correctAgainst:continuedFrom:

Like correctAgainst:. Use when you want to correct against several lists, give nil as the first oldCollection, and nil as the last wordList.


 

correctAgainstDictionary:continuedFrom:

Like correctAgainst:continuedFrom:. Use when you want to correct against a dictionary.


 

encodeForHTTP

change dangerous characters to their %XX form, for use in HTTP transactions


 

findSelector

Dan's code for hunting down selectors with keyword parts; while this doesn't give a true parse, in most cases it does what we want, in where it doesn't, we're none the worse for it.


 

initialIntegerOrNil

Answer the integer represented by the leading digits of the receiver, or nil if the receiver does not begin with a digit


 

keywords

Answer an array of the keywords that compose the receiver.


 

numericSuffix


 

romanNumber


 

sansPeriodSuffix

Return a copy of the receiver up to, but not including, the first period. If the receiver's *first* character is a period, then just return the entire receiver.


 

splitInteger

Answer an array that is a splitting of self into a string and an integer.
'43Sam' ==> #(43 'Sam'). 'Try90' ==> #('Try' 90)
BUT NOTE: 'Sam' ==> #('Sam' 0), and '90' ==> #('' 90) ie, (<string> <integer>).


 

stemAndNumericSuffix

Parse the receiver into a string-valued stem and a numeric-valued suffix. 6/7/96 sw


 

substrings

Answer an array of the substrings that compose the receiver.


 

surroundedBySingleQuotes

Answer the receiver with leading and trailing quotes.


 

translateFrom:to:table:

translate the characters in the string by the given table, in place


 

translateToLowercase

Translate all characters to lowercase, in place


 

translateToUppercase

Translate all characters to lowercase, in place


 

translateWith:

translate the characters in the string by the given table, in place


 

truncateTo:

return myself or a copy shortened to smallSize. 1/18/96 sw


 

truncateWithElipsisTo:

Return myself or a copy suitably shortened but with elipsis added


 

unparenthetically

If the receiver starts with (..( and ends with matching )..), strip them


 

unzipped


 

withBlanksCondensed

Return a copy of the receiver with leading/trailing blanks removed
and consecutive white spaces condensed.


 

withBlanksTrimmed

Return a copy of the receiver from which leading and trailing blanks have been trimmed.


 

withFirstCharacterDownshifted

Answer an object like the receiver but with first character downshifted if necesary


 

withNoLineLongerThan:

Answer a string with the same content as receiver, but rewrapped so that no line has more characters than the given number


 

withSeparatorsCompacted

replace each sequences of whitespace by a single space character


 

withoutLeadingDigits

Answer the portion of the receiver that follows any leading series of digits and blanks. If the receiver consists entirely of digits and blanks, return an empty string


 

withoutTrailingBlanks

Return a copy of the receiver from which trailing blanks have been trimmed.


 

withoutTrailingDigits

Answer the portion of the receiver that precedes any trailing series of digits and blanks. If the receiver consists entirely of digits and blanks, return an empty string


  copying top  
 

copyReplaceTokens:with:

Replace all occurrences of oldSubstring that are surrounded
by non-alphanumeric characters


 

deepCopy

DeepCopy would otherwise mean make a copy of the character; since
characters are unique, just return a shallowCopy.


 

padded:to:with:


  displaying top  
 

displayAt:

Display the receiver as a DisplayText at aPoint on the display screen.


 

displayOn:

Display the receiver on the given DisplayMedium. 5/16/96 sw


 

displayOn:at:

Show a representation of the receiver as a DisplayText at location
aPoint on aDisplayMedium.


 

displayProgressAt:from:to:during:

Display this string as a caption over a progress bar while workBlock is evaluated.

EXAMPLE (Select next 6 lines and Do It)
'Now here''s some Real Progress'
displayProgressAt: Sensor cursorPoint
from: 0 to: 10
during: [:bar |
1 to: 10 do: [:x | bar value: x.
(Delay forMilliseconds: 500) wait]].

HOW IT WORKS (Try this in any other language :-)
Since your code (the last 2 lines in the above example) is in a block,
this method gets control to display its heading before, and clean up
the screen after, its execution.
The key, though, is that the block is supplied with an argument,
named 'bar' in the example, which will update the bar image every
it is sent the message value: x, where x is in the from:to: range.


 

newTileMorphRepresentative


  encoding top  
 

getInteger32:


 

primGetInteger32:


 

primPutInteger32:at:


 

putInteger32:at:


  filter streaming top  
 

byteEncode:


 

putOn:


  internet top  
 

decodeMimeHeader

See RFC 2047, MIME Part Three: Message Header Extension for Non-ASCII Text.
Text containing non-ASCII characters is encoded by the sequence
=?character-set?encoding?encoded-text?=
Encoding is Q (quoted printable) or B (Base64), handled by Base64MimeConverter / RFC2047MimeConverter. The character-set (usually iso-8859-1) is ignored


 

decodeQuotedPrintable

Assume receiver is in MIME 'quoted-printable' encoding, and decode it.


 

isoToSqueak


 

replaceHtmlCharRefs


 

squeakToIso


 

unescapePercents

change each %XY substring to the character with ASCII value XY in hex. This is the opposite of #encodeForHTTP


 

withInternetLineEndings

change line endings from CR's to CRLF's. This is probably in
prepration for sending a string over the Internet


 

withSqueakLineEndings

assume the string is textual, and that CR, LF, and CRLF are all
valid line endings. Replace each occurence with a single CR


 

withoutQuoting

remove the initial and final quote marks, if present


  paragraph support top  
 

indentationIfBlank:

Answer the number of leading tabs in the receiver. If there are
no visible characters, pass the number of tabs to aBlock and return its value.


  printing top  
 

basicType

Answer a symbol representing the inherent type of the receiver


 

encodeDoublingQuoteOn:

Print inside string quotes, doubling inbedded quotes.


 

isLiteral

Answer whether the receiver has a literal text form recognized by the
compiler.


 

printOn:

Print inside string quotes, doubling inbedded quotes.


 

storeOn:

Print inside string quotes, doubling inbedded quotes.


 

stringRepresentation

Answer a string that represents the receiver. For most objects this is simply its printString, but for strings themselves, it's themselves, to avoid the superfluous extra pair of quotes. 6/12/96 sw


  private top  
 

correctAgainstEnumerator:continuedFrom:

The guts of correction, instead of a wordList, there is a block that should take another block and enumerate over some list with it.


 

replaceFrom:to:with:startingAt:

Primitive. This destructively replaces elements from start to stop in the receiver starting at index, repStart, in the collection, replacement. Answer the receiver. Range checks are performed in the primitive only. Optional. See Object documentation whatIsAPrimitive.


 

stringhash


  system primitives top  
 

compare:with:collated:

Return 1, 2 or 3, if string1 is <, =, or > string2, with the collating order of characters given by the order array.


 

findSubstring:in:startingAt:matchTable:

Answer the index in the string body at which the substring key first occurs, at or beyond start. The match is determined using matchTable, which can be used to effect, eg, case-insensitive matches. If no match is found, zero will be returned.

The algorithm below is not optimum -- it is intended to be translated to C which will go so fast that it wont matter.


 

numArgs

Answer either the number of arguments that the receiver would take if considered a selector. Answer -1 if it couldn't be a selector. Note that currently this will answer -1 for anything begining with an uppercase letter even though the system will accept such symbols as selectors. It is intended mostly for the assistance of spelling correction.


  testing top  
 

hasContentsInExplorer


 

isAllDigits

whether the receiver is composed entirely of digits


 

isAllSeparators

whether the receiver is composed entirely of separators


 

isString

Overridden to return true in String, natch


 

lastSpacePosition

Answer the character position of the final space or other separator character in the receiver, and 0 if none


  user interface top  
 

asExplorerString


 

openInWorkspaceWithTitle:

Open up a workspace with the receiver as its contents, with the given title


class methods
  examples top  
 

example

To see the string displayed at the cursor point, execute this expression
and select a point by pressing a mouse button.


 

exampleFor:

Provide a MethodCall with a valid example of aSelector, ready to run. If 'all', provide all tests we know about for SUnit tests. When 'all', each will be a Verifier, with an expected answer. Returns a list of MethodCalls, Verifiers, or VerifierOfPropertys.


  initialization top  
 

initialize

String initialize


 

initializeHtmlEntities

String initializeHtmlEntities


  instance creation top  
 

cr

Answer a string containing a single carriage return character.


 

crlf

Answer a string containing a carriage return and a linefeed.


 

crlfcrlf


 

fromPacked:

Convert from a longinteger to a String of length 4.


 

fromString:

Answer an instance of me that is a copy of the argument, aString.


 

lf

Answer a string containing a single carriage return character.


 

readFrom:

Answer an instance of me that is determined by reading the stream,
inStream. Embedded double quotes become the quote Character.


 

tab

Answer a string containing a single tab character.


  internet top  

  plugin generation top  
 

ccg:prolog:expr:index:


 

ccgDeclareCForVar:


  primitives top  
 

findFirstInString:inSet:startingAt:


 

indexOfAscii:inString:startingAt:


 

stringHash:initialHash:


 

translate:from:to:table:

translate the characters in the string by the given table, in place