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

https://github.com/hernanmd/stringextensions

Add useful String extension method to Pharo String class
https://github.com/hernanmd/stringextensions

levenshtein pharo pharo-smalltalk smalltalk string string-manipulation string-matching

Last synced: 8 months ago
JSON representation

Add useful String extension method to Pharo String class

Awesome Lists containing this project

README

          

# Table of Contents

- [Description](#description)
- [Installation](#installation)
- [Install from CLI](#install-from-cli)
- [Install from Pharo](#install-from-pharo)
- [Baseline Snippet](#baseline-snippet)
- [Details](#details)
- [Summary](#summary)
- [Extraction Methods](#extraction-methods)
- [Conversion Methods](#conversion-methods)
- [Testing Methods](#testing-methods)
- [Implementation notes](#implementation-note)
- [License](#license)

# Description

Add missing useful String extension method to Pharo String class.

# Installation

## Install from CLI

Install **StringExtensions** from Command-Line Interface using [Pharo Install](https://github.com/hernanmd/pi):

```bash
pi install StringExtensions
```

## Install from Pharo

```smalltalk
EpMonitor disableDuring: [
Metacello new
baseline: 'StringExtensions';
repository: 'github://hernanmd/StringExtensions/repository';
load ].
```

## Baseline Snippet

If you want to add it to your Metacello Baseline, copy and paste the following expression:

```smalltalk
" ... "
baseline: 'StringExtensions'
with: [ spec repository: 'github://hernanmd/StringExtensions/repository' ].
" ... "
```

# Details

To quickly find methods provided by this package, please evaluate:

```smalltalk
(String methods select: #isExtension) select: [ : p | p package name = 'StringExtensions' ]
```

## Summary

|**Method**|**Purpose**|
| ------------- | ------------- |
|asCondensedString|Return a copy of the receiver with leading/trailing blanks removed and white spaces condensed|
|asFloat|Answer a that represents the value of the receiver|
|beginsWith:or:|Answer if receiver's begins with firstPrefix or secondPrefix|
|copyUpToAny:|Answer a discarding all elements in the receiver which appear after any of of the elements in aCollection|
|copyUpToStartParenthesis|Answer the receiver without preserving all Character's up to the first opening parenthesis|
|findNumbers|Answer a of numbers, removing other characters|
|hammingDistanceTo:|Answer a respresenting the minimum amount of substitutions to convert the receiver into aString|
|includesBeginWith:|Answer whether aString begins like one of the receiver's sub strings elements|
|includesEndsWith:|Answer whether aString ends like one of the receiver's sub strings elements|
|indexesOfMotif:|Answer a of indexes of a motif in a string|
|indicesOfCharacter:|Given aCharacter contained in the receiver, answer a of positions where the aCharacter is found|
|indicesOfSubstring:|Given aCharacter contained in the receiver, answer a of positions where the aCharacter is found|
|indicesOfSubstring:mismatches:|Answer a of start positions where subString is found allowing at most d mismatches|
|indicesOfSubstringOverlaps:|Given aCharacter contained in the receiver, answer a of positions where the aCharacter is found|
|isAllBlanks|Answer whether the receiver is composed entirely of space separators|
|isAllLetters|Answer whether the receiver is composed entirely of letters|
|isNumeric|Answer whether the receiver is a Number|
|isXML|Only answer when the receiver's contents *looks like* XML, not performing any validity check|
|isZipped|Answer if receiver conforms to GZIP file format|
|levenshteinDistanceTo:|Iterative calculation of the Levenshtein distance between two strings|
|lowercaseFirstLetter|Answer a String made up from the receiver whose first character is lowercase|
|shingle:|Answer an with contiguous sequences (shingles) of n characters|
|shingleMax:|Answer an maximally shingled with contiguous sequences (shingles) of n characters|
|shingleWords:|Word shingling|
|withoutBlanks|Return a copy of the receiver with leading/trailing blanks removed and white spaces condensed|
|withoutCRs|Answer the receiver without carriage returns|
|withoutNumbers|Return a copy of the receiver with numbers removed|

## Extraction Methods

### findNumbers

Extracting numbers in Strings is easily supported in Pharo:

```smalltalk
'Hola123Mundo' asInteger. "123"
```

However, consider the following:

```smalltalk
'Hola123Mundo234' asInteger. "123"
```

If we want to extract all numbers in the received **String**, then we could use #findNumbers:

```smalltalk
'Hola123Mundo234' "an OrderedCollection('123' '234')"
```

- On empty **String**, answer an empty **Collection**
- Negative Integers are extracted with sign: `'Hola-10' findNumbers. "an OrderedCollection('-10')"`
- Floats are not supported: `'Hola30.6' findNumbers. "an OrderedCollection('30' '6')"`

### withoutBlanks

Return a copy of the receiver with leading/trailing blanks removed and white spaces condensed.

### withoutNumbers

Reject all digits from the received **String**. Examples:

```smalltalk
'3333' withoutNumbers = ''.

'aaaa' withoutNumbers = 'aaaa'.

'aaa1234' withoutNumbers = 'aaa'.

'' withoutNumbers = ''
```

## Conversion Methods

### asCondensedString

Return a copy of the receiver with leading/trailing blanks removed and white spaces condensed.

### withoutCRs

Return a copy of the receiver without carriage returns.

## Testing Methods

### includesBeginWith:

Answer whether the parameter **String** begins like one of the receiver's sub strings elements. For example, the following expressions evaluate all to *true*:

```smalltalk
'Rose is a rose of splendor' includesBeginWith: 'rose'.

'Rose is a rose of splendor' includesBeginWith: 'ROSE'.

'Raise the art to resistance' includesBeginWith: 'resist'.

'Danger dare to be grand' includesBeginWith: 'dar'.
```

### isAllLetters

Answer *true* whether the receiver is composed entirely of letters.

### isNumeric

Answer whether the receiver is a **Number**. It uses a regular expression.

### isXML

Cheap checking of beggining XML document, no parsing involved, just check '