Documentation
String Functions
String manipulation and formatting helpers.
Utilities for working with and manipulating strings.
reverse()
Return the reverse of the given string
Implementation
Uses Python's string slicing with a negative step
Example
result = UniCoreFW.reverse("Hello")
Expected output: "olleH"
Source Code
def reverse(string: str) -> str:
"""
Return the reverse of the given string.
Args:
string: The string to reverse
Returns:
The reversed string
Example:
reverse("Hello") -> "olleH"
"""
return string[::-1]
camel_case()
Convert a string to camelCase
Implementation
Splits the string by spaces, underscores, and hyphens, then joins with first word lowercase and subsequent words capitalized
Example
result = UniCoreFW.camel_case("Hello world example")
Expected output: "helloWorldExample"
Source Code
def camel_case(string: str) -> str:
"""
Convert a string to camelCase.
Args:
string: The string to convert
Returns:
The camelCased version of the string
Example:
camel_case("Hello world example") -> "helloWorldExample"
"""
# Split the string into words, removing non-alphanumeric except underscores/spaces
words = re.split(r"[\s_\-]+", string.strip())
# First word is lowercased, subsequent words have capitalized first letter
if not words:
return ""
first_word = words[0].lower()
remaining = [w.capitalize() for w in words[1:]]
return first_word + "".join(remaining)
deburr()
Remove Latin-1 Supplement accents (U+00C0–U+00FF), leaving other scripts untouched.
Implementation
Args: string: The input text (None → "").
Returns:
A new string where each accented Latin-1 character is replaced
by its ASCII equivalent (e.g. “Æ”→“AE”, “ß”→“ss”), “×”/“÷” → space.
Example
deburr("déjà vu")
Expected output: "deja vu"
Source Code
def deburr(string: Any) -> str:
s = "" if string is None else str(string)
# Direct map for U+00C0–U+00FF
return "".join(_DEBURR_MAP.get(ch, ch) for ch in s)
snake_case()
Convert a string to snake_case
Implementation
Splits the string by spaces and hyphens, lowercases all words, and joins with underscores
Example
result = UniCoreFW.snake_case("Hello world example")
Expected output: "hello_world_example"
Source Code
def snake_case(string: str, flag: bool = True) -> str:
"""
Convert a string to snake_case.
Args:
string: The string to convert
flag: False to convert _____ to _
Returns:
The snake_cased version of the string
Example:
snake_case("Hello world example") -> "hello_world_example"
"""
# Lowercase everything, replace spaces/punctuation with underscores
if(flag):
words = re.split(r"[\s\-]+", string.strip().lower())
else:
words = re.split(r"[\s_\-]+", string.strip().lower())
return "_".join(word for word in words if word)
lower_case()
Convert a string to lower_case.
Implementation
This function is an implementation of the lowerCase function from
the Lodash library. It takes a string and converts it to lower_case,
inserting spaces between words and converting the string to lower case.
Args:
value: The string to convert to lower_case.
Returns:
The lower_cased version of the string.
Example
lower_case("Hello World")
Expected output: "hello world"
Source Code
def lower_case(value: Any) -> str:
if value is None:
return ""
s = str(value)
# reuse the boundary‐insertion from kebab_case
s = re.sub(r"([a-z\d])([A-Z])", r"\1 \2", s)
s = re.sub(r"(\d)([A-Za-z])", r"\1 \2", s)
s = re.sub(r"([A-Za-z])(\d)", r"\1 \2", s)
# split on non-alphanumeric
parts = re.split(r"[^0-9A-Za-z\u0080-\uFFFF]+", s)
return " ".join(p.lower() for p in parts if p)
kebab_case()
Convert a string to kebab-case
Implementation
Splits the string by spaces and underscores, lowercases all words, and joins with hyphens
Example
result = UniCoreFW.kebab_case("Hello world example")
Expected output: "hello-world-example"
Source Code
def kebab_case(string: str) -> str:
"""
Convert a string to kebab-case.
Args:
string: The string to convert
Returns:
The kebab-cased version of the string
Example:
kebab_case("Hello world example") -> "hello-world-example"
"""
words = re.split(r"[\s_\-]+", string.strip().lower())
return "-".join(word for word in words if word)
prune()
Prune string to exactly length characters (default 0) then append omission.
If a custom omission is provided that is longer than length, returns the original string.
Truncation always splits at word boundaries (spaces), dropping trailing punctuation.
Implementation
Args: string: The input text (None→""). length: Number of characters to keep before omission. Defaults to 0. omission: Text to append when pruning. Defaults to "...".
Returns:
Possibly‐pruned text.
Example
prune("Hello, world")
Expected output: "..."
Source Code
def prune(
string: Any,
length: Optional[int] = None,
omission: str = "..."
) -> str:
s = "" if string is None else str(string)
try:
keep = int(length) if length is not None else 0
except (TypeError, ValueError):
keep = 0
omit = "" if omission is None else omission
# If custom omission too long, skip pruning
if omission not in (None, "...") and len(omit) > keep:
return s
# If input fits, or length==0, we still proceed:
if keep <= 0:
head = ""
elif len(s) <= keep:
return s
else:
head = s[:keep]
# If input shorter than or equal keep, no prune
if len(s) <= keep:
return s
# Try break at last space
idx = head.rfind(" ")
if idx != -1:
head = head[:idx]
# Strip trailing non-alphanumeric
while head and not head[-1].isalnum():
head = head[:-1]
return head + omit
truncate()
Truncate a string to the specified length and append an ellipsis (or custom suffix)
Implementation
Checks string length and adds the ellipsis if truncation is needed
Example
result = UniCoreFW.truncate("Hello, world!", 5)
Expected output: "Hello..."
Source Code
def truncate(string: str, length: int, ellipsis: str = "...") -> str:
"""
Truncate a string to the specified length and append an ellipsis (or custom suffix).
Args:
string: The string to truncate
length: Maximum allowed length before truncation
ellipsis: Optional suffix to indicate truncation
Returns:
The truncated string, possibly with ellipsis appended
Example:
truncate("Hello, world!", 5) -> "Hello..."
"""
if length < 0:
raise ValueError("Length cannot be negative.")
if len(string) <= length:
return string
# Make sure we don't cut in the middle of the ellipsis
return string[:length] + ellipsis
repeat()
Repeat string n times.
Implementation
Args: string: The value to repeat (None→""). n: Number of repetitions. If ≤0, returns "".
Returns:
The repeated string.
Example
repeat("foo", 3)
Expected output: "foofoofoo"
Source Code
def repeat(string: Any, n: int = 0) -> str:
s = "" if string is None else str(string)
try:
count = int(n)
except (TypeError, ValueError):
return ""
return s * max(0, count)
upper_case()
This function is an implementation of the upperCase function from
the Lodash library. It takes a string and converts it to upper case,
inserting spaces between words and converting the string to upper case.
Implementation
Args: value: The string to convert to upper_case.
Returns:
The upper_cased version of the string.
Example
upper_case("Hello World")
Expected output: "HELLO WORLD"
Source Code
def upper_case(value: Any) -> str:
if value is None:
return ""
s = str(value)
s = re.sub(r"([a-z\d])([A-Z])", r"\1 \2", s)
s = re.sub(r"(\d)([A-Za-z])", r"\1 \2", s)
s = re.sub(r"([A-Za-z])(\d)", r"\1 \2", s)
parts = re.split(r"[^0-9A-Za-z\u0080-\uFFFF]+", s)
return " ".join(p.upper() for p in parts if p)
starts_with()
Check if the string starts with the given prefix
Implementation
Uses Python's startswith method
Example
result = UniCoreFW.starts_with("Hello world", "He")
Expected output: True
Source Code
def starts_with(string: str, prefix: str) -> bool:
"""
Check if the string starts with the given prefix.
Args:
string: The string to check
prefix: The prefix to look for
Returns:
True if the string starts with prefix; otherwise False
Example:
starts_with("Hello world", "He") -> True
"""
return string.startswith(prefix)
reg_exp_js_replace()
Replace via JS‐style regex literal. - 'g' flag → replace all, otherwise only first. - 'i' flag → case‐insensitive.
Implementation
Args: text: The input text (None→""). js_literal: JS regex literal (None or ""→no-op). replacement: Replacement text (None→"").
Returns:
The resulting string.
Example
reg_exp_js_replace("abc", "/a/i", "X")
Expected output: "Xbc"
Source Code
def reg_exp_js_replace(
text: Any,
js_literal: Any,
replacement: Any
) -> str:
s = "" if text is None else str(text)
rep = "" if replacement is None else str(replacement)
pat, flags, is_global = _parse_js_regex("" if js_literal is None else str(js_literal))
try:
regex = re.compile(pat, flags)
except re.error:
return s
count = 0 if is_global else 1
return regex.sub(rep, s, count)
reg_exp_js_match()
Match JS-style regex literal against text, returning list of matches. Non-global returns at most one; global returns all.
Implementation
Implementation details are not available.
Example
Expected output: ``
Source Code
def reg_exp_js_match(text: Any, js_literal: Any) -> List[str]:
s = "" if text is None else str(text)
pat, flags, is_global = _parse_js_regex("" if js_literal is None else str(js_literal))
try:
regex = re.compile(pat, flags)
except re.error:
return []
if is_global:
return [m.group(0) for m in regex.finditer(s)]
m = regex.search(s)
return [m.group(0)] if m else []
unquote()
Remove matching surrounding quotes from a string.
Implementation
Args: string: The possibly-quoted text. quote_char: If given, only strips that character; otherwise strips matching ' or ".
Returns:
Unquoted string.
Example
unquote('"hello"')
Expected output: "hello"
Source Code
def unquote(string: Any, quote_char: Optional[str] = None) -> str:
s = "" if string is None else str(string)
if quote_char:
qc = str(quote_char)
if s.startswith(qc) and s.endswith(qc) and len(s) >= 2:
return s[1:-1]
return s
if len(s) >= 2 and s[0] == s[-1] and s[0] in {"'", '"', "`"}:
return s[1:-1]
return s
insert_substr()
Insert substring into string at position index.
Implementation
Args: string: The original text. index: The zero-based insertion point. substring: The text to insert.
Returns:
Modified string.
Example
insert_substr("hello", 2, "X")
Expected output: "heXllo"
Source Code
def insert_substr(string: Any, index: int, substring: Any) -> str:
s = "" if string is None else str(string)
sub = "" if substring is None else str(substring)
try:
idx = int(index)
except (TypeError, ValueError):
idx = 0
if idx < 0:
idx = 0
if idx > len(s):
idx = len(s)
return s[:idx] + sub + s[idx:]
trim()
Trim whitespace or specified characters from both ends of the string.
Implementation
Args: string: The string to trim. chars: Optional set of characters to remove; defaults to whitespace.
Returns:
The trimmed string.
Example
trim(" abc ")
Expected output: "abc"
Source Code
def trim(string: Any, chars: Optional[str] = None) -> str:
return trim_end(trim_start(string, chars), chars)
trim_end()
Trim whitespace or specified characters from the end of the string.
Implementation
Args: string: The string to trim. chars: Optional set of characters to remove; defaults to whitespace.
Returns:
The trimmed string.
Example
trim_end(" abc ")
Expected output: " abc"
Source Code
def trim_end(string: Any, chars: Optional[str] = None) -> str:
s = "" if string is None else str(string)
return s.rstrip() if chars is None else s.rstrip(chars)
trim_start()
Trim whitespace or specified characters from the start of the string.
Implementation
Args: string: The string to trim. chars: Optional set of characters to remove; defaults to whitespace.
Returns:
The trimmed string.
Example
trim_start(" abc ")
Expected output: "abc "
Source Code
def trim_start(string: Any, chars: Optional[str] = None) -> str:
s = "" if string is None else str(string)
return s.lstrip() if chars is None else s.lstrip(chars)
ensure_ends_with()
Ensure that string ends with suffix, adding it if missing.
Implementation
Args: string: The original text. suffix: The suffix to enforce.
Returns:
The string guaranteed to end with suffix.
Example
ensure_ends_with("hello", "!")
Expected output: "hello!"
Source Code
def ensure_ends_with(string: Any, suffix: Any) -> str:
s = "" if string is None else str(string)
suf = "" if suffix is None else str(suffix)
return s if (not suf or s.endswith(suf)) else s + suf
ensure_starts_with()
Ensure that string begins with prefix, adding it if missing.
Implementation
Args: string: The original text. prefix: The prefix to enforce.
Returns:
The string guaranteed to start with prefix.
Example
ensure_starts_with("world", "hello ")
Expected output: "hello world"
Source Code
def ensure_starts_with(string: Any, prefix: Any) -> str:
s = "" if string is None else str(string)
p = "" if prefix is None else str(prefix)
return s if (not p or s.startswith(p)) else p + s
escape_reg_exp()
Escape the characters that have special meaning in regular expressions.
Implementation
Args: string: The input to escape.
Returns:
A string with all RegExp metacharacters escaped.
Example
escape_reg_exp("[test](1.2)")
Expected output: "\[test\]\(1\.2\)"
Source Code
def escape_reg_exp(string: Any) -> str:
s = "" if string is None else str(string)
return re.escape(s)
separator_case()
Normalize string into lowercase “words” joined by sep. Splits on any
non-alphanumeric chars, collapses empties, lowercases.
Implementation
Args: string: The input text (None→""). sep: Separator to join (None→"").
Returns:
The normalized string.
Example
separator_case("Foo Bar", "-")
Expected output: "foo-bar"
Source Code
def separator_case(
string: Any,
sep: Any = '-'
) -> str:
text = "" if string is None else str(string)
sep_str = "" if sep is None else str(sep)
# split on any non-alphanumeric
parts = re.split(r'[^0-9A-Za-z]+', text)
words = [w.lower() for w in parts if w]
return sep_str.join(words)
replace_end()
Replace pattern at the end of string with replacement, once.
Implementation
Args: string: The input text (None→""). pattern: Substring or regex (last match only). replacement: Replacement text (None→"").
Returns:
The resulting string.
Example
replace_end("foo", "o", "a")
Expected output: "foa"
Source Code
def replace_end(
string: Any,
pattern: Any,
replacement: Any
) -> str:
text = "" if string is None else str(string)
if pattern is None:
return text
repl = "" if replacement is None else str(replacement)
# compile regex for end
if isinstance(pattern, re.Pattern):
regex = re.compile(rf'(?:{pattern.pattern})$')
else:
pat = re.escape(str(pattern))
regex = re.compile(rf'{pat}$')
return regex.sub(repl, text, 1)
replace_start()
Replace pattern at the start of string with replacement, once.
Implementation
Args: string: The input text (None→""). pattern: Substring or regex (first match only). replacement: Replacement text (None→"").
Returns:
The resulting string.
Example
replace_start("foo", "f", "a")
Expected output: "aoo"
Source Code
def replace_start(
string: Any,
pattern: Any,
replacement: Any
) -> str:
text = "" if string is None else str(string)
if pattern is None or replacement is None and replacement is not False:
repl = "" if replacement is None else str(replacement)
else:
repl = str(replacement)
# compile regex for start
if isinstance(pattern, re.Pattern):
regex = re.compile(rf'^(?:{pattern.pattern})')
else:
pat = re.escape(str(pattern)) if pattern is not None else ""
regex = re.compile(rf'^{pat}')
return regex.sub(repl, text, 1)
replace()
Replace up to count occurrences of pattern in string with replacement.
If count is 0 (default), replaces all occurrences.
If ignore_case is True, matching is case-insensitive.
pattern may be a plain string or a compiled regex.
Implementation
Args: string: The input text (None→""). pattern: Substring or regex to replace (None or ""→no-op). replacement: Replacement text (None→""). ignore_case: Case-insensitive match if True. count: Max replacements; 0 → unlimited.
Returns:
The resulting string.
Example
replace("foo", "o", "a")
Expected output: "faa"
Source Code
def replace(
string: Any,
pattern: Optional[Union[str, re.Pattern]],
replacement: Any,
ignore_case: bool = False,
count: int = 0
) -> str:
text = "" if string is None else str(string)
# no pattern → return original
if pattern is None:
return text
# coerce replacement
repl = "" if replacement is None else str(replacement)
# empty pattern → no-op
if isinstance(pattern, str) and pattern == "":
return text
# build regex
if isinstance(pattern, re.Pattern):
regex = pattern
else:
pat = str(pattern)
flags = re.IGNORECASE if ignore_case else 0
regex = re.compile(re.escape(pat), flags)
# do replace
return regex.sub(repl, text, count)
count_substr()
Count non-overlapping occurrences of substr in s.
Implementation
Args: s: The haystack (None → ""). substr: The needle (None → always 0). An empty string → len(haystack)+1.
Returns:
The number of occurrences of `substr` within `s`.
Example
count_substr("foobar", "o")
Expected output: 2
Source Code
def count_substr(s: Any, substr: Any) -> int:
text = "" if s is None else str(s)
if substr is None:
return 0
sub = str(substr)
if sub == "":
return len(text) + 1
return text.count(sub)
capitalize()
Uppercase the first character of s, with optional lowercasing of the rest.
Implementation
Args: s: The string or value to transform (None → ""). lower: If True (default), the remainder of the string is lowercased; if False, the remainder is left as-is.
Returns:
A string with its first character uppercased, and the rest
handled per the `lower` flag.
Example
capitalize("hello")
Expected output: "Hello"
Source Code
def capitalize(s: Any, lower: bool = True) -> str:
text = "" if s is None else str(s)
if not text:
return ""
first = text[0].upper()
rest = text[1:].lower() if lower else text[1:]
return first + rest
quote()
Wrap value (converted to string) with wrapper on both sides.
If wrapper is None, returns the raw string (or "" for None).
Implementation
Args:
value: The value to quote.
wrapper: Character or string to wrap around value.
Returns:
The quoted string.
Example
quote("foo")
Expected output: '"foo"'
Source Code
def quote(
value: Any,
wrapper: Optional[Any] = '"'
) -> str:
text = "" if value is None else str(value)
if wrapper is None:
return text
w = str(wrapper)
if not w:
return text
return f"{w}{text}{w}"
pad_end()
Pad string on the right side to reach length, using chars cyclically.
Implementation
Args: string: The original string (None treated as ""). length: Desired total length. chars: Characters to cycle for padding.
Returns:
The right-padded string.
Example
pad_end("a", 9, "12")
Expected output: "a12121212"
Source Code
def pad_end(
string: Optional[str],
length: int,
chars: str = " "
) -> str:
s = string or ""
if len(s) >= length:
return s
pad_count = length - len(s)
cycle = chars or " "
times = (pad_count + len(cycle) - 1) // len(cycle)
big = cycle * times
return s + big[:pad_count]
pad_start()
Pad string on the left side to reach length, using chars cyclically
(taking characters from the end of the cycle when truncating).
Implementation
Args: string: The original string (None treated as ""). length: Desired total length. chars: Characters to cycle for padding.
Returns:
The left-padded string.
Example
pad_start("a", 8, "12")
Expected output: "121212a"
Source Code
def pad_start(
string: Optional[str],
length: int,
chars: str = " "
) -> str:
s = string or ""
if len(s) >= length:
return s
pad_count = length - len(s)
cycle = chars or " "
times = (pad_count + len(cycle) - 1) // len(cycle)
big = cycle * times
# take last pad_count chars
return big[-pad_count:] + s
ends_with()
Check if the string ends with the given suffix
Implementation
Uses Python's endswith method
Example
result = UniCoreFW.ends_with("Hello world", "world")
Expected output: True
Source Code
def ends_with(string: str, suffix: str) -> bool:
"""
Check if the string ends with the given suffix.
Args:
string: The string to check
suffix: The suffix to look for
Returns:
True if the string ends with suffix; otherwise False
Example:
ends_with("Hello world", "world") -> True
"""
return string.endswith(suffix)
words()
Split the string into an array of words based on a regex pattern
Implementation
Uses regex split with an optional pattern parameter
Example
result = UniCoreFW.words("Hello, world!")
Expected output: ["Hello,", "world!"]
Source Code
def words(string: str, pattern: Optional[str] = None) -> List[str]:
"""
Split the string into an array of words based on a regex pattern.
If no pattern is provided, splits on whitespace.
Args:
string: The string to split
pattern: Optional regex pattern for splitting
Returns:
A list of words in the string
Example:
words("Hello, world!") -> ["Hello,", "world!"]
"""
if pattern is None:
pattern = r"\s+" # Default splits on whitespace
return [w for w in re.split(pattern, string) if w]
series_phrase()
Join a sequence into a human‐readable series without Oxford comma.
Implementation
Args: items: Iterable of values (None→empty list). sep: Delimiter between all but last two items. last_sep: Delimiter between the penultimate and last item.
Returns:
A joined string:
[] → ""
["foo"] → "foo"
["foo","bar"] → "foo and bar"
["a","b","c"] → "a, b and c"
Example
series_phrase(["a","b","c"])
Expected output: "a, b and c"
Source Code
def series_phrase(
items: Any,
sep: Any = ", ",
last_sep: Any = " and "
) -> str:
# Normalize inputs
seq = list(items) if items is not None else []
sep_str = "" if sep is None else str(sep)
last_str = "" if last_sep is None else str(last_sep)
# Filter out None or empty-string
parts = [str(x) for x in seq if x is not None and str(x) != ""]
n = len(parts)
if n == 0:
return ""
if n == 1:
return parts[0]
if n == 2:
return parts[0] + last_str + parts[1]
# n >= 3
return sep_str.join(parts[:-1]) + last_str + parts[-1]
lines()
Split a string into lines, handling Unix (\n), Windows (\r\n) and old Mac (\r) breaks.
Implementation
Drops a trailing empty line if one is produced (so "foo\n" → ["foo"], not ["foo", ""]).
Args:
string: The string to split, or None.
Returns:
A list of lines.
Example
lines("foo\nbar")
Expected output: ["foo", "bar"]
Source Code
def lines(string: Optional[str]) -> List[str]:
if string is None:
return []
# Normalize all CRLF or CR to LF
text = string.replace('\r\n', '\n').replace('\r', '\n')
parts = text.split('\n')
# Drop trailing empty line if present
if parts and parts[-1] == "":
parts.pop()
return parts
humanize()
Convert a snake_case, kebab-case, or underscored string into a human-readable form
Implementation
Replaces underscores and dashes with spaces and capitalizes the first letter
Example
result = UniCoreFW.humanize("hello_world_example")
Expected output: "Hello world example"
Source Code
def humanize(string: str) -> str:
"""
Convert a snake_case, kebab-case, or underscored string into a human-readable form.
For instance, it replaces underscores/dashes with spaces and capitalizes the first letter.
Args:
string: The string to humanize
Returns:
A human-readable version of the string
Example:
humanize("hello_world_example") -> "Hello world example"
"""
# Replace underscores or dashes with spaces
text = re.sub(r"[_\-]+", " ", string.strip())
return text.capitalize()
slice()
Return a substring of 'string' from index 'start' up to, but not including, 'end'
Implementation
Uses Python's array slicing syntax
Example
result = UniCoreFW.slice("Hello world", 1, 5)
Expected output: "ello"
Source Code
def slice(string: str, start: int = 0, end: Optional[int] = None) -> str:
"""
Return a substring of 'string' from index 'start' up to, but not including, 'end'.
If 'end' is None, slices until the end of the string.
Args:
string: The string to slice
start: Starting index
end: Ending index (non-inclusive), defaults to None
Returns:
The sliced substring
Example:
slice("Hello world", 1, 5) -> "ello"
"""
return string[start:end]
replace_all()
Replace all occurrences of 'find' in 'string' with 'replacement'
Implementation
Uses Python's string replace method
Example
result = UniCoreFW.replace_all("banana", "a", "o")
Expected output: "bonono"
Source Code
def replace_all(string: str, find: str, replacement: str) -> str:
"""
Replace all occurrences of 'find' in 'string' with 'replacement'.
Args:
string: The original string
find: The substring to find
replacement: The string to replace 'find' with
Returns:
The string with replacements applied
Example:
replace_all("banana", "a", "o") -> "bonono"
"""
return string.replace(find, replacement)
regex_find_all()
Find all non-overlapping matches of a regex pattern in the given string
Implementation
Uses re.findall for pattern matching
Example
result = UniCoreFW.regex_find_all("abc123xyz456", r"\d+")
Expected output: ["123", "456"]
Source Code
def regex_find_all(string: str, pattern: str, flags: int = 0) -> List[str]:
"""
Find all non-overlapping matches of a regex pattern in the given string.
Args:
string: The string to search.
pattern: The regular expression pattern to match.
flags: Optional re.* flags (e.g., re.IGNORECASE, re.MULTILINE).
Returns:
A list of all non-overlapping matches in the string.
Example:
regex_find_all("abc123xyz456", r"\\d+") -> ["123", "456"]
"""
return re.findall(pattern, string, flags=flags)
regex_test()
Test if the string contains at least one match of the regex pattern
Implementation
Uses re.search and converts the result to a boolean
Example
result = UniCoreFW.regex_test("Hello123", r"\d+")
Expected output: True
Source Code
def regex_test(string: str, pattern: str, flags: int = 0) -> bool:
"""
Test if the string contains at least one match of the regex pattern.
Args:
string: The string to test.
pattern: The regular expression pattern to match.
flags: Optional re.* flags (e.g., re.IGNORECASE, re.MULTILINE).
Returns:
True if there's at least one match; otherwise False.
Example:
regex_test("Hello123", r"\\d+") -> True
"""
return bool(re.search(pattern, string, flags=flags))
regex_replace()
Replace all matches of the given regex pattern in 'string' with 'replacement'
Implementation
Uses re.sub, supporting both string and function replacements
Example
result = UniCoreFW.regex_replace("abc123", r"\d+", "#")
Expected output: "abc#"
Source Code
def regex_replace(
string: str,
pattern: str,
replacement: Union[str, Callable[[re.Match], str]],
flags: int = 0
) -> str:
"""
Replace all matches of the given regex pattern in 'string' with 'replacement'.
'replacement' can be either a regular string or a callable (match object -> string).
If it's a callable, each match object is passed for dynamic replacement.
Args:
string: The string in which to perform the replacements.
pattern: The regex pattern to find.
replacement: A string or callable used to replace each match.
flags: Optional re.* flags (e.g., re.IGNORECASE, re.MULTILINE).
Returns:
A new string with all replacements applied.
Example:
regex_replace("abc123", r"\\d+", "#") -> "abc#"
regex_replace("abc123", r"[a-z]", lambda m: m.group(0).upper()) -> "ABC123"
"""
return re.sub(pattern, replacement, string, flags=flags)
regex_extract()
Find the first match of 'pattern' in 'string' and return the specified capture group
Implementation
Uses re.search and extracts the requested group
Example
result = UniCoreFW.regex_extract("Name: Alice, Age: 30", r"Name:\s+(\w+)", 1)
Expected output: "Alice"
Source Code
def regex_extract(
string: str,
pattern: str,
group: int = 0,
flags: int = 0
) -> Optional[str]:
"""
Find the first match of 'pattern' in 'string' and return the specified capture group.
Args:
string: The string to search.
pattern: The regex pattern to match.
group: The capture group index to return (default 0 = entire match).
flags: Optional re.* flags (e.g., re.IGNORECASE, re.MULTILINE).
Returns:
The matched substring if found; otherwise None.
Example:
regex_extract("Name: Alice, Age: 30", r"Name:\\s+(\\w+)", 1) -> "Alice"
"""
match = re.search(pattern, string, flags=flags)
return match.group(group) if match else None
regex_extract_all()
Find all non-overlapping matches of 'pattern' in 'string' and return the specified capture group
Implementation
Uses re.finditer and collects the specified group from each match
Example
result = UniCoreFW.regex_extract_all("Name: Alice, Name: Bob, Name: Charlie", r"Name:\s+(\w+)", 1)
Expected output: ["Alice", "Bob", "Charlie"]
Source Code
def regex_extract_all(
string: str,
pattern: str,
group: int = 0,
flags: int = 0
) -> List[str]:
"""
Find all non-overlapping matches of 'pattern' in 'string' and return the specified capture group.
Args:
string: The string to search.
pattern: The regex pattern to match.
group: The capture group index to return (default 0 = entire match).
flags: Optional re.* flags (e.g., re.IGNORECASE, re.MULTILINE).
Returns:
A list of matched substrings, possibly empty if no matches are found.
Example:
regex_extract_all("Name: Alice, Name: Bob, Name: Charlie",
r"Name:\\s+(\\w+)", 1)
-> ["Alice", "Bob", "Charlie"]
"""
matches = re.finditer(pattern, string, flags=flags)
return [m.group(group) for m in matches]
strip_html_tags()
Remove all HTML tags from a string
Implementation
Uses regex to remove anything within angle brackets
Example
result = UniCoreFW.strip_html_tags("<p>Hello <em>world</em>!</p>")
Expected output: "Hello world!"
Source Code
def strip_html_tags(string: str) -> str:
"""
Remove all HTML tags from a string.
Args:
string: The string to process.
Returns:
A copy of the string with all HTML tags removed.
Example:
strip_html_tags("<p>Hello <em>world</em>!</p>")
-> "Hello world!"
"""
# This pattern finds anything of the form <...>
# Note: This is a simplistic approach; for complex HTML parsing,
# consider a dedicated HTML parser.
return re.sub(r"<[^>]*>", "", string)
slugify()
Convert a string into a URL-friendly slug
Implementation
Lowercases, removes non-alphanumerics, and replaces spaces with delimiters
Example
result = UniCoreFW.slugify("Hello, World!")
Expected output: "hello-world"
Source Code
def slugify(string: str, delimiter: str = "-") -> str:
"""
Convert a string into a URL-friendly slug.
1. Lowercases the string
2. Removes non-alphanumeric characters (except spaces)
3. Replaces whitespace with the given delimiter
Args:
string: The string to slugify.
delimiter: The delimiter to replace spaces, defaults to '-'.
Returns:
A URL-friendly slug string.
Example:
slugify("Hello, World!") -> "hello-world"
"""
# Lowercase
text = string.lower().strip()
# Remove characters except alphanumerics and spaces
text = re.sub(r"[^a-z0-9\s]+", "", text)
# Replace whitespace with the chosen delimiter
return re.sub(r"\s+", delimiter, text)
join()
Join elements of an array into a string. None elements become empty strings.
Implementation
Args: array: Iterable of elements (or None) separator: Separator to insert between elements (defaults to "")
Returns:
The joined string
Example
join(["foo", "bar", "baz"], "_")
Expected output: "foo_bar_baz"
Source Code
def join(array, separator=None) -> str:
if array is None:
return ""
sep = "" if separator is None else str(separator)
parts = []
for x in array:
if x is None:
parts.append("")
else:
parts.append(str(x))
return sep.join(parts)
human_case()
Convert a string into human-readable form: - Trims whitespace - Drops trailing 'id' suffix (with optional underscore or hyphen) - Splits on capitals, underscores, hyphens - Lowercases and capitalizes first letter
Implementation
Args: string: The input string
Returns:
A human-cased string
Example
human_case("helloWorld_test")
Expected output: "Hello world test"
Source Code
def human_case(string: str) -> str:
if string is None:
return ""
s = str(string).strip()
if not s:
return ""
# drop trailing id
s = re.sub(r"[_\-\s]*id$", "", s, flags=re.IGNORECASE)
# split CamelCase (including XMLHttp → XML Http)
s = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1 \2", s)
s = re.sub(r"([a-z0-9])([A-Z])", r"\1 \2", s)
# replace separators with spaces
s = re.sub(r"[_\-]+", " ", s)
s = s.lower().strip()
if not s:
return ""
return s[0].upper() + s[1:]
url()
Combine URL parts into a single URL with optional query parameters.
Implementation
Args: parts: URL components (strings). params: Optional query parameters as keyword args.
Returns:
Combined URL string.
Example
link = url("a", "b", ["c", "d"], "/", q="X", y="Z")
path, params = link.split("?")
path == "a/b/c/d/"
Expected output: True
Source Code
def url(*parts: str, **params: Any) -> str:
if not parts:
return ""
u = urlsplit(parts[0])
scheme, netloc, path, query, frag = u.scheme, u.netloc, u.path, u.query, u.fragment
query_items = parse_qsl(query, keep_blank_values=True)
for part in parts[1:]:
p = urlsplit(part)
# Merge queries
query_items.extend(parse_qsl(p.query, keep_blank_values=True))
if frag:
# Merge into fragment if one exists
frag_path = p.fragment or p.path.strip("/")
if frag_path:
frag += "/" + frag_path
else:
# Merge into path if no fragment yet
if p.fragment:
frag = p.fragment
if p.path:
path = path.rstrip("/") + "/" + p.path.lstrip("/")
# Ensure trailing slash if last part had it
if parts[-1].endswith("/") and not path.endswith("/"):
path += "/"
# Merge keyword arguments into query string
for k, v in params.items():
if isinstance(v, (list, tuple)):
query_items.extend((k, str(i)) for i in v)
else:
query_items.append((k, str(v)))
return urlunsplit((scheme, netloc, path, urlencode(query_items, doseq=True), frag))
reg_exp_replace()
Replace occurrences of regex pattern with replacement in text. Optionally, ignore case
when replacing. Optionally, set count to limit number of replacements.
Implementation
Args:
text: String to replace.
pattern: Pattern to find and replace.
replacement: String to substitute pattern with.
ignore_case: Whether to ignore case when replacing. Defaults to False.
count: Maximum number of occurrences to replace. Defaults to None which
replaces all.
Returns:
Replaced string.
Example
reg_exp_replace("aabbcc", "b", "X")
Expected output: 'aaXXcc'
Source Code
def reg_exp_replace(
text: Any,
pattern: Any,
replacement: Any,
ignore_case: bool = False,
count: Optional[int] = None
) -> str:
s = "" if text is None else str(text)
if pattern is None or replacement is None:
return s
rep = str(replacement)
# empty‐pattern special
pat_str = None
if not isinstance(pattern, re.Pattern):
pat_str = str(pattern)
if pat_str == "":
# every boundary
parts = list(s)
return rep + rep.join(parts) + rep
flags = re.IGNORECASE if ignore_case else 0
# compile from string or accept existing pattern
try:
regex = re.compile(pat_str if pat_str is not None else pattern, flags)
except re.error:
return s
# count=None → all, else up to count
return regex.sub(rep, s, 0 if count is None else max(0, int(count)))
mask_sensitive()
Mask sensitive information by replacing pattern matches with the specified character(s)
Implementation
Uses regex with a replacement function to mask matched sequences
Example
result = UniCoreFW.mask_sensitive("Card: 1234-5678-9012-3456", r"\d")
Expected output: "Card: ****-****-****-****"
Source Code
def mask_sensitive(string: str, pattern: str, mask_char: str = "*") -> str:
"""
Mask sensitive information by replacing pattern matches with the specified character(s).
Great for hiding phone numbers, credit-card details, etc.
Args:
string: The string to process.
pattern: A regex pattern capturing what should be masked.
mask_char: The character (or string) to replace each matched group with.
Returns:
A new string where every match of pattern is replaced by mask_char repeated
for the length of each match.
Example:
# Hide digits with a simple pattern (all digits)
mask_sensitive("Card: 1234-5678-9012-3456", r"\\d")
-> "Card: ****-****-****-****"
"""
def mask_match(m: re.Match) -> str:
return mask_char * len(m.group(0))
return re.sub(pattern, mask_match, string)
highlight_matches()
Surround each regex match with highlight markers
Implementation
Uses regex with a custom replacement function to wrap matched text
Example
result = UniCoreFW.highlight_matches("Hello, hello!", "hello", "<<", ">>", re.IGNORECASE)
Expected output: "<<Hello>>, <<hello>>!"
Source Code
def highlight_matches(
string: str,
pattern: str,
highlight_start: str = "<<",
highlight_end: str = ">>",
flags: int = 0
) -> str:
"""
Surround each regex match with highlight markers.
Args:
string: The input text.
pattern: The regex pattern to highlight.
highlight_start: The prefix for each match.
highlight_end: The suffix for each match.
flags: Regex flags (e.g., re.IGNORECASE).
Returns:
A new string where all matches are wrapped in highlight markers.
Example:
highlight_matches("Hello, HELLO, hello!", "hello", "<<", ">>", re.IGNORECASE)
-> "<<Hello>>, <<HELLO>>, <<hello>>!"
"""
def wrap_match(m: re.Match) -> str:
return f"{highlight_start}{m.group(0)}{highlight_end}"
return re.sub(pattern, wrap_match, string, flags=flags)
normalize_whitespace()
Replace multiple consecutive whitespace characters with a single space, and trim leading/trailing whitespace
Implementation
Uses regex to convert all whitespace sequences to a single space
Example
result = UniCoreFW.normalize_whitespace(" Too many spaces\n here\t.")
Expected output: "Too many spaces here ."
Source Code
def normalize_whitespace(string: str) -> str:
"""
Replace multiple consecutive whitespace characters (spaces, tabs, newlines)
with a single space, and trim leading/trailing whitespace.
Args:
string: The string to normalize.
Returns:
A new string with normalized spacing.
Example:
normalize_whitespace(" Too many spaces\n here\t.")
-> "Too many spaces here ."
"""
# Collapse consecutive whitespace to a single space
text = re.sub(r"\s+", " ", string)
# Strip leading/trailing whitespace
return text.strip()
dedent_text()
Remove any common leading whitespace from every line in the string
Implementation
Uses textwrap.dedent from the standard library
Example
result = UniCoreFW.dedent_text(" def function():\n pass")
Expected output: "def function():\n pass"
Source Code
def dedent_text(string: str) -> str:
"""
Remove any common leading whitespace from every line in the string.
Args:
string: The string to dedent.
Returns:
A new string with shared leading whitespace removed.
Example:
text = \"""
def function():
pass
\"""
dedent_text(text) -> "def function():\n pass"
"""
return textwrap.dedent(string)
pad()
Pad string on both sides to reach the given total length, using chars cyclically.
Implementation
Args: string: The original string (None treated as ""). length: Desired total length of the output string. chars: Characters to cycle for padding.
Returns:
The padded string. If `string` is longer than `length`, returns it unchanged.
Example
pad("abc", 9, "12")
Expected output: "1abc1"
Source Code
def pad(
string: Optional[str],
length: int,
chars: str = " "
) -> str:
s = string or ""
if len(s) >= length:
return s
pad_total = length - len(s)
left = pad_total // 2
right = pad_total - left
# build a long enough pad string and slice
def build(n):
cycle = chars or " "
times = (n + len(cycle) - 1) // len(cycle)
return (cycle * times)[:n]
return build(left) + s + build(right)
has_substr()
Check whether substr occurs in s, starting the search at index pos.
Implementation
Args: s: The string or value to search (None→""). substr: The substring to look for (None or "" → always True). pos: The index to begin searching from.
Returns:
True if `substr` is found in `s` at or after `pos`, otherwise False.
Example
has_substr("abc", "b")
Expected output: True
Source Code
def has_substr(s: Any, substr: Any, pos: int = 0) -> bool:
text = "" if s is None else str(s)
needle = "" if substr is None else str(substr)
if needle == "":
return True
try:
return text.find(needle, pos) != -1
except Exception:
return False
to_upper()
Convert s to all uppercase.
Implementation
Args: s: The string (or value) to transform.
Returns:
Uppercased string. If `s` is None, returns "".
Example
to_upper("FooBar")
Expected output: "FOOBAR"
Source Code
def to_upper(s: Any) -> str:
return "" if s is None else str(s).upper()
to_lower()
Convert s to all lowercase.
Implementation
Args: s: The string (or value) to transform.
Returns:
Lowercased string. If `s` is None, returns "".
Example
to_lower("FooBar")
Expected output: "foobar"
Source Code
def to_lower(s: Any) -> str:
return "" if s is None else str(s).lower()
title_case()
Convert s to Title Case (capitalize first letter of each word, rest lowercase).
Implementation
Args: s: The string (or value) to transform.
Returns:
A title-cased string. If `s` is None, returns "".
Example
title_case("hello world")
Expected output: "Hello World"
Source Code
def title_case(s: Any) -> str:
s = "" if s is None else str(s)
def cap(word: str) -> str:
return word[0].upper() + word[1:].lower() if word else ""
return " ".join(cap(w) for w in s.split(" "))
swap_case()
Swap the case of each letter in s.
Implementation
Args: s: The string (or value) to transform.
Returns:
A new string where lowercase letters are uppercased and vice versa.
Non-letter characters are unchanged. If `s` is None, returns "".
Example
swap_case("Hello World")
Expected output: "hELLO wORLD"
Source Code
def swap_case(s: Any) -> str:
s = "" if s is None else str(s)
return "".join(
ch.lower() if ch.isupper() else ch.upper() if ch.islower() else ch
for ch in s
)
surround()
Surround value with wrapper on both sides.
Implementation
Args: value: The inner content (any type). wrapper: The string (or value) to wrap around.
Returns:
A new string: wrapper + value + wrapper. If `wrapper` is None,
returns only the stringified `value`. If `value` is None, treats
as empty string.
Example
surround("foo", "**")
Expected output: '**foo**'
Source Code
def surround(value: Any, wrapper: Any) -> str:
val = "" if value is None else str(value)
if wrapper is None:
return val
wrap = str(wrapper)
return f"{wrap}{val}{wrap}"
successor()
Return the character immediately following the first character of s in Unicode.
Implementation
Args: s: A one-character string (or value).
Returns:
The successor character. If `s` is None or empty, returns an empty string.
Example
successor("a")
Expected output: "b"
Source Code
def successor(s: Any) -> str:
s = "" if s is None else str(s)
if not s:
return ""
return chr(ord(s[0]) + 1)
predecessor()
Return the character immediately preceding the first character of s in Unicode.
Implementation
Args: s: A one-character string (or value).
Returns:
The predecessor character. If `s` is None or empty, returns an empty string.
Example
predecessor("a")
Expected output: "Z"
Source Code
def predecessor(s: Any) -> str:
s = "" if s is None else str(s)
if not s:
return ""
return chr(ord(s[0]) - 1)
strip_tags()
Remove all HTML tags from the string.
Implementation
Args: s: The string (or value) to clean.
Returns:
The string with all `<...>` tags stripped out. If `s` is None or
empty, returns an empty string.
Example
strip_tags("<h1>Hello, world!</h1>")
Expected output: "Hello, world!"
Source Code
def strip_tags(s: Any) -> str:
from .string import strip_html_tags # existing helper
s = "" if s is None else str(s)
return strip_html_tags(s)
substr_right_end()
Return the substring after the last occurrence of sep.
Implementation
Args: s: The string (or value) to inspect. sep: The separator to look for.
Returns:
The portion of `s` after the last `sep`. If `sep` is empty or None,
returns the entire string. If `s` is None, returns an empty string.
Example
substr_right_end("a_b_c", "_")
Expected output: "c"
Source Code
def substr_right_end(s: Any, sep: Any) -> str:
s = "" if s is None else str(s)
if not sep:
return s
sep = str(sep)
idx = s.rfind(sep)
return s if idx == -1 else s[idx + len(sep):]
substr_right()
Return the substring after the first occurrence of sep.
Implementation
Args: s: The string (or value) to inspect. sep: The separator to look for.
Returns:
The portion of `s` after the first `sep`. If `sep` is empty or None,
returns the entire string. If `s` is None, returns an empty string.
Example
substr_right("a_b_c", "_")
Expected output: "b_c"
Source Code
def substr_right(s: Any, sep: Any) -> str:
s = "" if s is None else str(s)
if not sep:
return s
sep = str(sep)
idx = s.find(sep)
return s if idx == -1 else s[idx + len(sep):]
substr_left_end()
Return the substring before the last occurrence of sep.
Implementation
Args: s: The string (or value) to inspect. sep: The separator to look for.
Returns:
The portion of `s` up to (but not including) the last `sep`. If `sep`
is empty or None, returns the entire string. If `s` is None, returns
an empty string.
Example
substr_left_end("a_b_c", "_")
Expected output: "a_b"
Source Code
def substr_left_end(s: Any, sep: Any) -> str:
s = "" if s is None else str(s)
if not sep:
return s
sep = str(sep)
idx = s.rfind(sep)
return s if idx == -1 else s[:idx]
substr_left()
Return the substring before the first occurrence of sep.
Implementation
Args: s: The string (or value) to inspect. sep: The separator to look for.
Returns:
The portion of `s` before the first `sep`. If `sep` is empty or None,
returns the entire string. If `s` is None, returns an empty string.
Example
substr_left("a_b_c", "_")
Expected output: "a"
Source Code
def substr_left(s: Any, sep: Any) -> str:
s = "" if s is None else str(s)
if not sep:
return s
sep = str(sep)
idx = s.find(sep)
return s if idx == -1 else s[:idx]
chars()
Return a list of the individual characters in the string.
Implementation
Args: s: The string (or other value) to split into chars.
Returns:
A list of single-character strings. None or "" → [].
Example
chars("foobarbaz")
Expected output: ["f", "o", "o", "b", "a", "r", "b", "a", "z"]
Source Code
def chars(s: Any) -> List[str]:
text = _to_str(s)
return list(text) if text else []
clean()
Collapse all whitespace (spaces, tabs, line breaks) to single spaces, and trim leading/trailing whitespace.
Implementation
Args: s: The string (or other value) to clean.
Returns:
A cleaned string with no extra spaces. None → "".
Example
clean(" Too many spaces
Expected output: here .")
"Too many spaces here ."
Source Code
def clean(s: Any) -> str:
text = _to_str(s)
# collapse any run of whitespace into a single space, then trim
return re.sub(r"\s+", " ", text).strip()
chop_right()
Split the string into chunks of length size, starting from the right.
The first chunk may be shorter if the string length isn’t a multiple of size.
Implementation
Args: s: The string (or other value) to chop from the right. size: The chunk length. If size ≤ 0, returns the whole string as a single chunk.
Returns:
A list of chunks. Returns [] if the input is None or empty.
Example
chop_right("foobarbaz", 3)
Expected output: ["foo", "bar", "baz"]
Source Code
def chop_right(s: Any, size: int) -> List[str]:
text = _to_str(s)
if not text or size <= 0:
return [] if not text else [text]
n = len(text)
rem = n % size
chunks: List[str] = []
# first chunk handles the remainder
start = 0
if rem:
chunks.append(text[0:rem])
start = rem
# remaining full-size chunks
for i in range(start, n, size):
chunks.append(text[i : i + size])
return chunks
chop()
Split the string into chunks of length size, starting from the front.
The last chunk may be shorter if the string length isn’t a multiple of size.
Implementation
Args: s: The string (or other value) to chop. size: The chunk length. If size ≤ 0, returns the whole string as a single chunk.
Returns:
A list of chunks. Returns [] if the input is None or empty.
Example
chop("foobarbaz", 3)
Expected output: ["foo", "bar", "baz"]
Source Code
def chop(s: Any, size: int) -> List[str]:
text = _to_str(s)
if not text or size <= 0:
return [] if not text else [text]
result: List[str] = []
for i in range(0, len(text), size):
result.append(text[i : i + size])
return result
upper_first()
Uppercase the first character of s, leaving the rest untouched.
Implementation
Args: s: The string or value to transform.
Returns:
String with first character uppercased.
Example
upper_first("hello")
Expected output: "Hello"
Source Code
def upper_first(s: Any) -> str:
return capitalize(s)
lower_first()
Lowercase the first character of s, leaving the rest of the string untouched.
Implementation
Args: s: The string or value to transform.
Returns:
String with first character lowercased.
Example
lower_first("Hello")
Expected output: "hello"
Source Code
def lower_first(s: Any) -> str:
return decapitalize(s)
pascal_case()
Convert s to PascalCase (also known as UpperCamelCase): each word capitalized, concatenated.
Implementation
Args: s: The string or value to transform.
Returns:
PascalCased string.
Example
pascal_case("foo bar")
Expected output: "FooBar"
Source Code
def pascal_case(s: Any) -> str:
s = _to_str(s)
words = re.split(r'[^0-9A-Za-z]+', s)
return ''.join(capitalize(w) for w in words if w)
decapitalize()
Convert the first character of s to lowercase, leaving the rest unchanged.
Implementation
Args: s: The string or value to transform.
Returns:
A new string with first letter lowercase, rest as in original.
Example
decapitalize("Foo")
Expected output: "foo"
Source Code
def decapitalize(s: Any) -> str:
s = _to_str(s)
if not s:
return ""
return s[0].lower() + s[1:]