https://github.com/rtmigo/string_trimmer_py
Removing unwanted parts from strings
https://github.com/rtmigo/string_trimmer_py
Last synced: 10 months ago
JSON representation
Removing unwanted parts from strings
- Host: GitHub
- URL: https://github.com/rtmigo/string_trimmer_py
- Owner: rtmigo
- License: mit
- Created: 2022-01-04T20:52:18.000Z (about 4 years ago)
- Default Branch: staging
- Last Pushed: 2022-01-05T01:14:49.000Z (about 4 years ago)
- Last Synced: 2025-01-21T15:32:13.626Z (12 months ago)
- Language: Python
- Homepage:
- Size: 26.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [string_trimmer](https://github.com/rtmigo/string_trimmer_py)
`TripleTrimmer` removes unwanted parts from a string.
## Examples
```python3
from string_trimmer import TripleTrimmer
trimmer = TripleTrimmer(
prefixes=["mr.", "mrs.", " "],
suffixes=[" esq", " phd", "."])
print(trimmer.shortest("Mr. John Doe Esq.".lower()))
# john doe
```
```python3
from string_trimmer import TripleTrimmer
trimmer = TripleTrimmer(
suffixes=["'ll"],
words=["he", "she", "they", "the", "a", "an"])
words = [trimmer.shortest(word) for word
in "she'll eat an ice cream".split()]
print(words)
# ['', 'eat', '', 'ice', 'cream']
```
## Install
### pip
```bash
pip3 install git+https://github.com/rtmigo/string_trimmer_py#egg=string_trimmer
```
### setup.py
```python3
install_requires = [
"string_trimmer@ git+https://github.com/rtmigo/string_trimmer_py"
]
```
## TripleTrimmer
`TripleTrimmer` removes unwanted parts from a string.
* Parts from the `prefixes` list will only be removed at the beginning of the
string
* Parts from the `suffixes` list will only be removed at the end of the string
* Parts from the `words` list - will turn the string into empty if it is equal
to any of the `words` elements
Since the unwanted parts can be of different lengths, the trimmed strings can
also be different.
The `trim` method will try to trim the word in every possible way, repeating
attempts recursively. The result will be a list of strings.
The `shortest` method returns a single string: the first of the shortest strings
returned by `trim`.
## PrefixTrimmer and SuffixTrimmer
These objects act exactly like the TripleTrimmer, but only remove the
corresponding parts of the strings.