Skip to content
XulbuX edited this page Sep 11, 2025 · 2 revisions

String

This class includes methods for working with strings.


to_type()

This method will try to convert a string to a fitting Python type or structure (a string "{'key': ['val1', 'val2']}" becomes a dictionary {"key": ["val1", "val2"]}).
Param:string: str the string to be converted to its type
Returns: the string as a type


normalize_spaces()

This method will replace all special space characters with normal spaces.
Params:

  • string: str the string to normalize
  • tab_spaces: int = 4 the amount of spaces to replace tab characters with (default 4)

Returns: the string with all spaces normalized


escape()

This method will escape all of Python's special characters (e.g. \n, \t, ...) and quotes inside a string.
Params:

  • string: str the string to escape
  • str_quotes: str = '"' the quotes, the string will later be inside of

Returns: The string with special characters and quotes escaped

String Quotes Escaping:

If the string is later placed in double-quotes like "some string" then str_quotes should be ".
The same thing goes for a string like 'another string', in which case str_quotes should be '.

The quotes will be escaped the following way:

  1. If str_quotes is " and the given string is some "string" inside a string, the quotes will be escaped, so the output is some \"string\" inside a string.
  2. If str_quotes is ', though, and the given string is some "string" inside a string, the quotes will not be escaped because their not the same type of quotes.

is_empty()

This method will simply check if the input is None or an empty string (or a string consisting only of spaces).
Params:

  • string: str the string to check if it's empty
  • spaces_are_empty: bool = False whether to check if the string is only spaces

Returns:True if the string is empty, False otherwise


single_char_repeats()

This method checks if the given string consists of only the same character and if yes, returns the number of times that character is present.
Params:

  • string: str the string to check
  • char: str the character to check for

Returns:
if the string consists of only the same char, it returns the number of times the character is present
if the string doesn't consist of only the same character, it returns False


decompose()

This method will decompose a cased string into its individual parts.
Params:

  • case_string: str the string to decompose
  • seps: str = "-_" the characters that separate the parts
  • lower_all: bool = True whether to lowercase all the parts

Returns: a list with the decomposed string parts


to_camel_case()

This method will convert a string of any type of casing to UpperCamelCase or lowerCamelCase.
Params:

  • string: str the string to convert
  • upper: bool = True whether to convert to UpperCamelCase

Returns: the string with the new casing


to_delimited_case()

This method will convert a string of any type of casing to a delimited string.
Params:

  • string: str the string to convert
  • delimiter: str = "_" the delimiter to use
  • screaming: bool = False whether to use screaming case (all parts in uppercase)

Returns: the string with the new delimited casing


get_lines()

This method will split the given string by newlines and optionally remove empty lines.
Params:

  • string: str the string to split
  • remove_empty_lines: bool = False whether to remove empty lines

Returns: a list with the split lines


remove_consecutive_empty_lines()

This method will remove consecutive empty lines from the given string, up to max_consecutive empty lines.
Params:

  • string: str the string to remove consecutive empty lines from
  • max_consecutive: int = 0 the maximum amount of consecutive empty lines to keep

Returns: the string with consecutive empty lines removed


split_count()

This method will split the given string every count characters.
Params:

  • string: str the string to split
  • count: int the number of characters by which the string is split

Returns: a list with the split string parts

★⠀Python Library by XulbuX⠀★

Project Links

Testing and Formatting

Classifiers

  • Intended Audience:
    • Developers
  • License:
    • OSI Approved
    • MIT License
  • Operating Systems:
    • Full Library: OS Independent
  • Supported Python Versions:
    • Python 3.13
    • Python 3.12
    • Python 3.11
    • Python 3.10
  • Topics:
    • Libraries
    • Python Modules
    • Software Development

The XulbuX Logo
Clone this wiki locally