String Library Reference
The string library in EasyBite offers a rich set of functions for working with text. You can use these functions either by calling them directly on string values (e.g. "hello".count()) or by importing the string module and invoking them as string.count("hello"), or by importing specific functions with from string import count.
Available Functions
| Function | Parameters | Description |
|---|---|---|
count(str) | str (String) | Returns the number of characters in str. |
contains(str, sub) | str, sub (Strings) | Returns true if sub is found in str, otherwise false. |
replace(str, old, new) | str, old, new (Strings) | Returns a new string where all occurrences of old are replaced with new. |
substring(str, start, end) | str (String), start, end (Numbers) | Returns the substring of str from character index start up to (but not including) end. |
uppercase(str) | str (String) | Returns str converted to all uppercase letters. |
lowercase(str) | str (String) | Returns str converted to all lowercase letters. |
capitalize(str) | str (String) | Returns str with the first letter of each word capitalized. |
reverse(str) | str (String) | Returns str with its characters in reverse order. |
join(arr, sep) | arr (Array of Strings), sep (String) | Returns a single string made by concatenating the elements of arr, separated by sep. |
tolist(str, sep) | str, sep (Strings) | Splits str on each occurrence of sep and returns an array of substrings. |
compare(str1, str2) | str1, str2 (Strings) | Returns -1 if str1 < str2, 0 if equal, 1 if str1 > str2. |
trim(str) | str (String) | Returns str with leading and trailing whitespace removed. |
startswith(str, prefix) | str, prefix (Strings) | Returns true if str begins with prefix, otherwise false. |
endswith(str, suffix) | str, suffix (Strings) | Returns true if str ends with suffix, otherwise false. |
strremove(str, sub) | str, sub (Strings) | Returns a new string with all occurrences of sub removed from str. |
split(str, sep) | str, sep (Strings) | Alias for tolist(str, sep). Splits str into an array on sep. |
find(str, sub) | str, sub (Strings) | Returns the zero-based index of the first occurrence of sub in str, or -1 if not found. |
Examples
Below are detailed examples for each function, shown in two styles: direct method calls on string values and imported function calls.
count(str)
show("Hello, world!".count())
// Output: 13
import string
show(string.count("Hello, world!"))
// Output: 13
from string import count
show(count("Hello, world!"))
// Output: 13
contains(str, sub)
show("EasyBite".contains("Bite"))
// Output: true
import string
show(string.contains("EasyBite", "bite"))
// Output: false // case-sensitive
replace(str, old, new)
show("foo bar baz".replace("ba", "BA"))
// Output: foo BAr BAz
import string
show(string.replace("foo bar foo", "foo", "qux"))
// Output: qux bar qux
substring(str, start, end)
show("EasyBite".substring(0, 4))
// Output: Easy
import string
show(string.substring("Hello, world!", 7, 12))
// Output: world
uppercase(str)
show("hello".uppercase())
// Output: HELLO
import string
show(string.uppercase("Hello, EasyBite!"))
// Output: HELLO, EASYBITE!
lowercase(str)
show("HELLO".lowercase())
// Output: hello
import string
show(string.lowercase("Hello, EasyBite!"))
// Output: hello, easybite!
capitalize(str)
show("hello easybite".capitalize())
// Output: Hello Easybite
import string
show(string.capitalize("welcome to easybite"))
// Output: Welcome To Easybite
reverse(str)
show("abcde".reverse())
// Output: edcba
import string
show(string.reverse("EasyBite"))
// Output: etibysaE
join(arr, sep)
show(["a","b","c"].join("-"))
// Output: a-b-c
import string
show(string.join(["2021","04","19"], "/"))
// Output: 2021/04/19
tolist(str, sep)
show("a,b,c".tolist(","))
// Output: ["a","b","c"]
import string
show(string.tolist("one|two|three", "|"))
// Output: ["one","two","three"]
compare(str1, str2)
show("apple".compare("banana"))
// Output: -1
import string
show(string.compare("apple", "apple"))
// Output: 0
trim(str)
show(" padded ".trim())
// Output: padded
import string
show(string.trim("\t tabbed line \n"))
// Output: tabbed line
startswith(str, prefix)
show("EasyBite".startswith("Easy"))
// Output: true
import string
show(string.startswith("EasyBite", "Bite"))
// Output: false
endswith(str, suffix)
show("filename.txt".endswith(".txt"))
// Output: true
import string
show(string.endswith("filename.txt", ".jpg"))
// Output: false
strremove(str, sub)
show("foo bar foo".strremove("foo"))
// Output: " bar "
import string
show(string.strremove("banana", "na"))
// Output: ba
split(str, sep)
show("2021-04-19".split("-"))
// Output: ["2021","04","19"]
import string
show(string.split("a|b|c", "|"))
// Output: ["a","b","c"]
find(str, sub)
show("hello".find("l"))
// Output: 2
import string
show(string.find("hello", "z"))
// Output: -1
Conclusion
The string library in EasyBite provides a comprehensive toolbox for all your text‑processing needs. You can:
- Call these methods directly on string values, for example
"text".count(). - Import the entire module with
import stringand usestring.method(str, ...). - Import specific functions with
from string import count, replaceto call them directly.
This flexibility, combined with the clear, consistent syntax of EasyBite, makes string manipulation both powerful and straightforward.