An open API service indexing awesome lists of open source software.

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

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.