String

Inherits from: Object

An immutable sequence of characters.

Strings are created with single-quote literals: 'hello'. Concatenate with the comma operator. Compare with =, <, >, <=, >=. Indices are 0-based.

Test
'hello' size >>> 5
'hello', ' world' >>> 'hello world'
'hello' at: 0 >>> $h
Example
"Build a greeting"
| name <String> |
name := 'Maggie'.
'Hello, ', name, '!'

Class Methods

primitives

class primLf

uncategorized

class lf

Return a string containing a single newline character (LF). Class method — call as String lf.

Test
String lf size >>> 1

Instance Methods

primitives

asFloat

Parse the receiver as a float.

asInteger

Parse the receiver as an integer.

hashCode

Return an FNV-1a hash of the string content.

matchesRegex:
primAsLowercase
primAsSymbol
primAsUppercase
primAt:
primConcat:
primCopyFrom:to:
primDo:
primEquals:
primIncludes:
primIndexOf:
primIsDigit
primIsLetter
primIsWhitespace
primLessThan:
primSize
primSplit:
primTrimBoth
print

Print the receiver to stdout.

println

Print the receiver to stdout followed by a newline.

splitRegex:

uncategorized

,

Return a new string that is the concatenation of the receiver and other.

Test
'hello', ' world' >>> 'hello world'
'', 'abc' >>> 'abc'
'x', '' >>> 'x'
<

Return true if the receiver sorts before other lexicographically.

<=

Return true if the receiver sorts before or equal to other.

=

Return true if the receiver and other contain the same characters.

Test
'abc' = 'abc' >>> true
'abc' = 'xyz' >>> false
>

Return true if the receiver sorts after other lexicographically.

>=

Return true if the receiver sorts after or equal to other.

asLowercase

Return a new string with all characters converted to lowercase.

Test
'HELLO' asLowercase >>> 'hello'
'Hello World' asLowercase >>> 'hello world'
asString

Return the receiver (strings are already strings).

Test
'hello' asString >>> 'hello'
asSymbol

Return the string as a symbol (interned string).

Test
'hello' asSymbol >>> #hello
asUppercase

Return a new string with all characters converted to uppercase.

Test
'hello' asUppercase >>> 'HELLO'
'Hello World' asUppercase >>> 'HELLO WORLD'
at:

Return the Character at a 0-based index.

Test
'hello' at: 0 >>> $h
'hello' at: 4 >>> $o
copyFrom:

Return a substring from start index to the end, 0-based.

Test
'hello' copyFrom: 2 >>> 'llo'
'hello' copyFrom: 0 >>> 'hello'
copyFrom:to:

Return a substring from start (inclusive) to end (exclusive), 0-based.

Test
'hello' copyFrom: 1 to: 4 >>> 'ell'
'hello' copyFrom: 0 to: 5 >>> 'hello'
do:

Evaluate aBlock with each character (as a single-character string).

Test
result := ''.
'abc' do: [:ch | result := result, ch, '-'].
result >>> 'a-b-c-'
endsWith:

Return true if the string ends with the given suffix.

Test
'hello' endsWith: 'llo' >>> true
'hello' endsWith: 'xyz' >>> false
includes:

Return true if the string contains the given substring or character.

Test
'hello' includes: 'ell' >>> true
'hello' includes: 'xyz' >>> false
indexOf:

Return the 0-based index of the first occurrence, or -1 if not found.

Test
'hello' indexOf: 'l' >>> 2
'hello' indexOf: 'z' >>> -1
isAlphanumeric

Return true if this single-character string is a letter or digit.

Test
'a' isAlphanumeric >>> true
'5' isAlphanumeric >>> true
' ' isAlphanumeric >>> false
isDigit

Return true if this single-character string is a digit (0-9).

Test
'5' isDigit >>> true
'a' isDigit >>> false
isEmpty

Return true if the string has no characters.

Test
'' isEmpty >>> true
'a' isEmpty >>> false
isLetter

Return true if this single-character string is a letter (a-z, A-Z).

Test
'a' isLetter >>> true
'5' isLetter >>> false
isWhitespace

Return true if this single-character string is whitespace.

Test
' ' isWhitespace >>> true
'a' isWhitespace >>> false
notEmpty

Return true if the string has at least one character.

Test
'hello' notEmpty >>> true
'' notEmpty >>> false
printString

Return a quoted string representation suitable for display.

Test
'hello' printString >>> '''hello'''
replaceAll:with:

Return a new string with all occurrences of old replaced by new.

Test
'hello world' replaceAll: 'o' with: '0' >>> 'hell0 w0rld'
'aaa' replaceAll: 'a' with: 'bb' >>> 'bbbbbb'
'hello' replaceAll: 'z' with: 'q' >>> 'hello'
'a-b-c' replaceAll: '-' with: '_' >>> 'a_b_c'
size

Return the number of characters in the string.

Test
'hello' size >>> 5
'' size >>> 0
split:

Split the string by the given separator and return an Array of substrings.

Test
'a,b,c' split: ',' >>> #('a' 'b' 'c' )
'hello' split: 'l' >>> #('he' '' 'o' )
'no-sep' split: ',' >>> #('no-sep' )
startsWith:

Return true if the string starts with the given prefix.

Test
'hello' startsWith: 'hel' >>> true
'hello' startsWith: 'xyz' >>> false
'--key' startsWith: '--' >>> true
trimBoth

Return a copy with leading and trailing whitespace removed.

Test
'  hello  ' trimBoth >>> 'hello'
'no spaces' trimBoth >>> 'no spaces'