Builtins and Libraries
3.1 Global Utilities
3.2 Numbers
3.3 Strings
3.4 Booleans
3.5 Raw  Array
3.6 Tables
3.7 lists
3.8 sets
3.9 arrays
3.10 string-dict
3.11 option
3.12 pick
3.13 either
3.14 srcloc
3.15 pprint
3.16 s-exp
3.17 s-exp-structs
3.18 image-structs
3.19 image
3.20 world
3.21 reactors
3.22 plot
On this page:
String
3.3.1 String Functions
string-equal
string-contains
string-append
string-length
string-to-number
string-repeat
string-substring
string-index-of
string-replace
string-split
string-split-all
string-explode
string-char-at
string-toupper
string-to-upper
string-tolower
string-to-lower
string-to-code-point
string-to-code-points
string-from-code-point
string-from-code-points

3.3 Strings

The type of string values

3.3.1 String Functions
string-equal :: (s1 :: String, s2 :: String) -> Boolean

Examples:

check: string-equal("abc", "abc") is true "abc" is%(string-equal) "abc" string-equal("ab", "abc") is false end

string-contains :: (string-to-search :: String, string-to-find :: String) -> Boolean

Examples:

check: string-contains("Ahoy, world!", "world") is true string-contains("Ahoy, World!", "world") is false string-contains("world", "Ahoy world") is false string-contains("same string", "same string") is true string-contains("", "") is true string-contains("any string", "") is true end

string-append :: (beginning :: String, end :: String) -> String

Examples:

check: string-append("a", "b") is "ab" string-append("same", "same") is "samesame" string-append("", "") is "" string-append("", "a") is "a" string-append("a", "") is "a" end

Returns the number of characters in the string.

Because Pyret currently uses a representation of strings that closely matches browsers’ behavior, string-length reports a count of 2 for code points over 65535.

Examples:

check: string-length("") is 0 string-length("four") is 4 string-length("𝄞") is 2 end

Converts the argument string to a number, returning none if it is not a valid numeric string, and a some<Number> if it is.

Examples:

check: string-to-number("100") is some(100) string-to-number("not-a-number") is none end

string-repeat :: (s :: String, n :: Number) -> String

Examples:

check: string-repeat("a", 5) is "aaaaa" string-repeat("", 1000000) is "" string-repeat("word ", 3) is "word word word " string-repeat("long string", 0) is "" end

string-substring :: (
s :: String,
start :: Number,
end :: Number
)
-> String

Returns a new string created from the characters of the input string, starting from start and ending at end. Raises an exception if start is greater than end, if start or end is greater than the length of the string, or if start or end is less than 0.

The returned string always has length end - start.

Examples:

check: string-substring("just the first", 0, 1) is "j" string-substring("same index", 4, 4) is "" tws = "length is 12" string-substring(tws, 4, 6) is "th" string-substring(tws, string-length(tws) - 1, string-length(tws)) is "2" string-substring(tws, 6, 4) raises "index" string-substring(tws, 6, 13) raises "index" string-substring(tws, 13, 6) raises "index" string-substring(tws, -1, 10) raises "index" end

string-index-of :: (original-string :: String, string-to-find :: String) -> Number

Returns the index from the beginning of the string where string-to-find first appears, or -1 if the string isn’t found.

Examples:

check: string-index-of("abcb", "b") is 1 string-index-of("", "b") is -1 end

string-replace :: (
original-string :: String,
string-to-find :: String,
replacement-string :: String
)
-> String

Examples:

check: string-replace("", "", "c") is "c" string-replace("spaces to hyphens", " ", "-") is "spaces-to-hyphens" string-replace("remove: the: colons", ":", "") is "remove the colons" string-replace("rinky dinky", "inky", "azzle") is "razzle dazzle" string-replace("a string", "not found", "not replaced") is "a string" string-replace("aaa", "", "b") is "bababab" end

string-split :: (original-string :: String, string-to-split-on :: String) -> List<String>

Searches for string-to-split-on in original-string. If it is not found, returns a List containing original-string as its single element.

If it is found, it returns a two-element List, whose first element is the portion of the string before first occurence of string-to-split-on. The second element contains the portion of the string after. The string-to-split-on is not included in either string. The string before and the string after might be empty.

For splitting beyond the first occurence of the string, see string-split-all.

Examples:

check: string-split("string", "not found") is [list: "string"] string-split("string", "g") is [list: "strin", ""] string-split("string", "") is [list: "", "string"] string-split("a-b-c", "-") is [list: "a", "b-c"] end

string-split-all :: (original-string :: String, string-to-split-on :: String) -> String

Searches for string-to-split-on in original-string. If it is not found, returns a List containing original-string as its single element.

If it is found, it returns a List, whose elements are the portions of the string that appear in between occurences of string-to-split-on. A match at the beginning or end of the string will add an empty string to the beginning or end of the list, respectively. The empty string matches in between every pair of characters.

Examples:

check: string-split-all("string", "not found") is [list: "string"] string-split-all("a-b-c", "-") is [list: "a", "b", "c"] string-split-all("split on spaces", " ") is [list: "split", "on", "spaces"] string-split-all("explode", "") is [list: "e", "x", "p", "l", "o", "d", "e"] string-split-all("bananarama", "na") is [list: "ba", "", "rama"] string-split-all("bananarama", "a") is [list: "b", "n", "n", "r", "m", ""] end

A shorthand for string-split-all(s, "").

string-char-at :: (s :: String, n :: Number) -> String

Examples:

check: string-char-at("abc", 1) is "b" string-char-at("a", 0) is "a" end

Pyret uses JavaScript’s built-in string operations, and so will have the same behavior as toUpperCase. Convert a string to all uppercase characters. Punctuation and other characters without an uppercase equivalent are left alone. Note that because of characters like ß, the length of the input is not guaranteed to match the length of the output.

Examples:

check: string-to-upper("a") is "A" string-to-upper("I'm not yelling!") is "I'M NOT YELLING!" string-to-upper("ß") is "SS" string-to-upper("λαμβδα") is "ΛΑΜΒΔΑ" end

When performing case-insensitive comparisons, it can be useful to convert both strings to uppercase first:

Examples:

check: string-to-upper("This Title may have been Capitalized Inconsistently") is string-to-upper("This Title May Have Been Capitalized Inconsistently") end

Examples:

check: string-to-lower("A") is "a" string-to-lower("I'M NOT YELLING!") is "i'm not yelling!" string-to-lower("SS") is "ss" string-to-lower("ΛΑΜΒΔΑ") is "λαμβδα" end

For strings that contain a single character whose code point is greater than 65535, this function raises an error. To get multiple codes at once for a longer string (or a string with larger code points), use string-to-code-points.

Converts s, which must be a single-character string, to a character code – a number corresponding to its unicode code point (http://en.wikipedia.org/wiki/Code_point).

Examples:

check: string-to-code-point("a") is 97 string-to-code-point("\n") is 10 string-to-code-point("λ") is 955 end

Converts the string (of any length) to a list of code points. Note that strings are encoded in such a way that some characters correspond to two code points (see the note in string-to-code-point).

Examples:

check: string-to-code-points("") is [list:] string-to-code-points("abc") is [list: 97, 98, 99] string-to-code-points("𝄞") is [list: 55348, 56606] end

Code points greater than 65535 are not supported. You must encode higher code points with a surrogate pair in combination with string-from-code-points and string-to-code-points.

Converts the code point code to a Pyret string.

Examples:

check: string-from-code-point(97) is "a" string-from-code-point(10) is "\n" string-from-code-point(955) is "λ" end

Converts from a list of code points to a Pyret string.

Examples:

check: string-from-code-points([list:]) is "" string-from-code-points([list: 97, 98, 99]) is "abc" string-from-code-points([list: 55348, 56606]) is "𝄞" end