3.3 Strings
The type of string values
3.3.1 String Functions
check: string-equal("abc", "abc") is true "abc" is%(string-equal) "abc" string-equal("ab", "abc") is false end
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
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.
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.
check: string-to-number("100") is some(100) string-to-number("not-a-number") is none end
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.
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
Returns the index from the beginning of the string where string-to-find first appears, or -1 if the string isn’t found.
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
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
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.
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
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.
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, "").
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.
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:
check: string-to-upper("This Title may have been Capitalized Inconsistently") is string-to-upper("This Title May Have Been Capitalized Inconsistently") end
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).
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).
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.
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.
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