Remove all consecutive characters or words from a string on Javascript, Typescript and Php

Our work as a developers sometimes requires us to perform string validation or cleanups to make sure that we are storing correctly formated texts. A very common use case is to clean a raw text by removing consecutive spaces, unwanted symbols or words and replace them with a single occurence.

For example, It is usual to get strings like "hello     world" where more than one empty space is used, and we want to filter it so there is only one to obtain "hello world" or we may want to get rid of several line breaks like in "hello\n\n\n\nworld" to obtain "hello\nworld". Other combinations like several digits or words that are found incorrectly repeated on a string may require our attention.

Using the TurboCommons library this process is as simple as calling a method and getting it's result.

Replacing consecutive characters with a single occurence with the TurboCommons library

There's a general purpose removeSameConsecutive() method inside the StringUtils class which lets you filter a text so all consecutive blocs of text get replaced with a single one. Let's see several examples with different programming languages:

Replace consecutive characters on a string in Php

Download the latest TurboCommons phar file from the downloads section, place it on your project as a dependency and run the following code:

require '%path-to-your-project-dependencies-folder%/turbocommons-php-X.X.X.phar';
use org\turbocommons\src\main\php\utils\StringUtils;
echo StringUtils::removeSameConsecutive('hello     world', [' '])."<br>";
echo StringUtils::removeSameConsecutive('hello.......world', ['.'])."<br>";
echo StringUtils::removeSameConsecutive('hellohello worldworld', ['hello', 'world'])."<br>";
echo StringUtils::removeSameConsecutive("hello\n\n\n\n\nworld", ["\n"])."<br>";

The output of the previous code should be:

hello world
hello.world
hello world
hello
world

Replace consecutive characters on a string on a website

Download the latest turbocommons-es5.js file from the downloads section or use npm to add the dependecy to your project (npm install turbocommons-es5). Then run the following code:

<script src="turbocommons-es5/turbocommons-es5.js"></script>
<script>
var StringUtils = org_turbocommons.StringUtils;
console.log(StringUtils.removeSameConsecutive('hello     world', [' ']));
console.log(StringUtils.removeSameConsecutive('hello.......world', ['.']));
console.log(StringUtils.removeSameConsecutive('hellohello worldworld', ['hello', 'world']));
console.log(StringUtils.removeSameConsecutive("hello\n\n\n\n\nworld", ["\n"]));
</script>

The console log for the previous code should be:

hello world
hello.world
hello world
hello
world

Replace consecutive characters on a string in Typescript (TS)

The recommended way is to use npm to obtain the turbocommons dependency by executing the following command at the root of your project:

npm install turbocommons-ts

Or you can download the latest turbocommons-ts files from the downloads section and copy the dependency by yourself. Then run the following code:

import { StringUtils } from 'turbocommons-ts';
console.log(StringUtils.removeSameConsecutive('hello     world', [' ']));
console.log(StringUtils.removeSameConsecutive('hello.......world', ['.']));
console.log(StringUtils.removeSameConsecutive('hellohello worldworld', ['hello', 'world']));
console.log(StringUtils.removeSameConsecutive("hello\n\n\n\n\nworld", ["\n"]));

The output of the previous code should be:

hello world
hello.world
hello world
hello
world

Replace consecutive characters on a string in a NodeJs App

Install the dependency by executing the following command at the root of your project:

npm install turbocommons-ts

And then run the following code:

const {StringUtils} = require('turbocommons-ts');
console.log(StringUtils.removeSameConsecutive('hello     world', [' ']));
console.log(StringUtils.removeSameConsecutive('hello.......world', ['.']));
console.log(StringUtils.removeSameConsecutive('hellohello worldworld', ['hello', 'world']));
console.log(StringUtils.removeSameConsecutive("hello\n\n\n\n\nworld", ["\n"]));

Which should output:

hello world
hello.world
hello world
hello
world