Skip to content

String Module ​

Provides utility functions for advanced string manipulation.
Includes case conversions, indentation and whitespace normalization, safe type parsing, character frequency analysis, and code pattern detection.

to_type() ​

python
def to_type(string: str, /) -> Any

Will convert a string to the found type, including complex nested structures.


  • string – The string to convert.

Example Usage ​

python
import xulbux as xx

val1 = xx.string.to_type("12345")
val2 = xx.string.to_type("[1, 2, 3]")
val3 = xx.string.to_type('{"enabled": true, "timeout": 30}')
Parsed Types
python
val1 = 12345  # int
val2 = [1, 2, 3]  # list[int]
val3 = {"enabled": True, "timeout": 30}  # dict[str, Any]

normalize_spaces() ​

python
def normalize_spaces(string: str, /, tab_spaces: int = 4) -> str

Replaces all special space characters with normal spaces.


  • string – The string to normalize.
  • tab_spaces – Number of spaces to replace tab chars with.

escape() ​

python
def escape(string: str, /, str_quotes: Literal["'", '"'] | None = None) -> str

Escapes Python's special characters (e.g., \n, \t, …) and quotes inside the string.


  • string – The string to escape.
  • str_quotes – The type of quotes the string will be put inside of (or None to not escape quotes):
    Can be either " or ' and should match the quotes, the string will be put inside of.
    So if your string will be "string", str_quotes should be ".
    That way, if the string includes the same quotes, they will be escaped.

is_empty() ​

python
def is_empty(string: str | None, /, *, spaces_are_empty: bool = False) -> bool

Returns True if the string is considered empty and False otherwise.


  • string – The string to check (or None, which is considered empty).
  • spaces_are_empty – If true, strings consisting only of spaces are also considered empty.

count_char_repeats() ​

python
def count_char_repeats(string: str, char: str, /) -> int
  • If the string consists of only the same char, it returns the number of times it is present.
  • If the string is empty or doesn't consist of only the same character, it returns 0.

  • string – The string to check.
  • char – The character to check for repetition.

decompose() ​

python
def decompose(string: str, /, seps: str = "-_", *, lower_all: bool = True) -> list[str]

Will decompose the string (any type of casing, also mixed) into parts.


  • string – The string to decompose.
  • seps – Additional separators to split the string at.
  • lower_all – If true, all parts will be converted to lowercase.

Example Usage ​

python
import xulbux as xx

parts = xx.string.decompose("myHTTPServer_port-config")
Decomposed Parts
python
["my", "http", "server", "port", "config"]

to_camel_case() ​

python
def to_camel_case(string: str, /, *, upper: bool = True) -> str

Will convert the string of any type of casing to CamelCase.


  • string – The string to convert.
  • upper – If true, it will convert to UpperCamelCase, otherwise to lowerCamelCase.

Example Usage ​

python
import xulbux as xx

upper_camel = xx.string.to_camel_case("my_var_name")  # MyVarName
lower_camel = xx.string.to_camel_case("my_var_name", upper=False)  # myVarName

to_delimited_case() ​

python
def to_delimited_case(string: str, /, delimiter: str = "_", *, screaming: bool = False) -> str

Will convert the string of any type of casing to delimited case.


  • string – The string to convert.
  • delimiter – The delimiter to use between parts.
  • screaming – Whether to convert all parts to uppercase.

Example Usage ​

python
import xulbux as xx

kebab = xx.string.to_delimited_case("MyVarName", delimiter="-")  # my-var-name
screaming_snake = xx.string.to_delimited_case("MyVarName", screaming=True)  # MY_VAR_NAME

get_lines() ​

python
def get_lines(string: str, /, *, remove_empty_lines: bool = False) -> list[str]

Will split the string into lines.


  • string – The string to split.
  • remove_empty_lines – If true, it will remove all empty lines from the result.

remove_consecutive_empty_lines() ​

python
def remove_consecutive_empty_lines(string: str, /, max_consecutive: int = 0) -> str

Will remove consecutive empty lines from the string.


  • string – The string to process.
  • max_consecutive – The maximum number of allowed consecutive empty lines:
    • If 0, it will remove all consecutive empty lines.
    • If bigger than 0, it will only allow max_consecutive consecutive empty lines and everything above it will be cut down to max_consecutive empty lines.

chunk() ​

python
def chunk(string: str, chunk_size: int, /) -> list[str]

Splits the string into chunks of chunk_size characters.


  • string – The string to split.
  • chunk_size – The number of characters per chunk.

Example Usage ​

python
import xulbux as xx

chunks = xx.string.chunk("abcdefghi", 3)  # ["abc", "def", "ghi"]

add_indent() ​

python
def add_indent(string: str, indent: int, /) -> str

Adds indent spaces at the beginning of each line.


  • string – The string to indent.
  • indent – The amount of spaces to add at the beginning of each line.

get_tab_spaces() ​

python
def get_tab_spaces(string: str, /) -> int

Will try to get the amount of spaces used for indentation.


  • string – The string to analyze.

change_tab_spaces() ​

python
def change_tab_spaces(string: str, space_count: int, /, *, remove_empty_lines: bool = False) -> str

Rescales indentation across lines to use space_count spaces per indentation level.


  • string – The string to modify the indentation of.
  • space_count – The new number of spaces per indentation level.
  • remove_empty_lines – If true, empty lines will be removed in the process.

extract_func_calls() ​

python
def extract_func_calls(code: str, /) -> list[tuple[str, str]]

Will try to get all function calls and return them as a list.


  • code – The code to analyze.

Example Usage ​

python
import xulbux as xx

code_snippet = "result = math.sqrt(foo.bar(10)) + calculate()"
calls = xx.string.extract_func_calls(code_snippet)
Extracted Calls
python
[
    ("sqrt", "foo.bar(10)"),
    ("calculate", ""),
    ("bar", "10"),
]

is_js() ​

python
def is_js(code: str, /, *, funcs: set[str] | frozenset[str] = _DEFAULT_JS_FUNCS) -> bool

Will check if the code is very likely to be JavaScript.


  • code – The code to analyze.
  • funcs – A list of custom function names to check for.

Example Usage ​

python
import xulbux as xx

js_code = "const add = (a, b) => { return a + b; };"
result = xx.string.is_js(js_code)
Result
python
True

Released under the MIT License.