from dataclasses import dataclass, InitVar from typing import Iterator type Str = 'String' | str @dataclass class String: _s: InitVar[str] def __post_init__(self, s: str) -> None: self.__s = s def __str__(self) -> str: return self.__s def __add__(self, other: Str) -> 'String': return String(self.__s + str(other)) def __iadd__(self, other: Str) -> 'String': return self + other def __len__(self) -> int: return len(self.__s) def __eq__(self, other: Str) -> bool: return str(self) == str(other) def concat(self, other: Str) -> None: self + other def contains(self, other: Str) -> bool: s = str(other) for c in str(self): if len(s) <= 0: break elif c == s[0]: s: str = s[1:] else: s = str(other) return len(s) == 0 def concat(self, other: Str) -> None: self.__s += str(other) def substring(self, start: int, end: int) -> 'String': substr: str = "" for i, c in enumerate(self.__s): if i >= start and i <= end: substr += c return substr def strip(self, chars: Str = ' ' + '\n' + '\t' + '\r') -> None: i = 0 while i < len(self) and self.__s[i] in chars: i += 1 j: int = len(self) - 1 while i <= j and self.__s[j] in chars: j -= 1 self.__s: str = str(self)[i:j + 1] def replace(self, old: Str, new: Str, count = -1) -> None: o = str(old) n = str(new) j = 0 while count > 0 or (count < 0 and j < len(self)): i: int = j while len(o) > 0 and j < len(self): if o[0] == self.__s[j]: o: str = o[1:] j += 1 else: j += 1 break if len(o) <= 0: self.__s = self.__s[:i] + n + self.__s[j:] j += len(new) - len(old) count -= 1 o = str(old) def add_prefix(self, prefix: Str) -> None: self.__s = str(prefix) + self.__s def add_suffix(self, suffix: Str) -> None: self.__s += str(suffix) def join[T](self, xs: Iterator[T]) -> 'String': output = String("") length: int = len(xs) for i, e in enumerate(xs): output += str(e) if i < length - 1: output += self return output