How to Replicate Python Stripe Method in JavaScript

Mehvish Ashiq Feb 02, 2024
  1. Replicate Python stripe() Method in JavaScript
  2. Use jQuery to Simulate Python stripe() Method in JavaScript
How to Replicate Python Stripe Method in JavaScript

This tutorial talks about simulating the Python stripe method in JavaScript. The Python stripe() removes white spaces from both edges (start and end) of the string.

For instance, " hello " turns into "hello". To get the same effect in JavaScript, we can use trim() and replace() methods.

The trim() is a feature of ECMAScript5 (ES5) that removes the white spaces from the start and end of the string. It is easily available in all the latest browsers.

The trim() has two variations to discard white space from the beginning only or from the end of the string. For that, we can use trimStart() and trimEnd() to eliminate spaces from start and end, respectively.

In case we also want to remove all spaces, including those that are separating one word from the other, then we can use the replace() method with regular expression.

The replace() function looks for a particular value in a string, replaces it and returns a new string without updating the original string.

Replicate Python stripe() Method in JavaScript

var str1 = ' hello world';
var str2 = 'hello world ';
var str3 = ' hello world ';
var str4 = '           hello world ';
console.log(str1.trimStart());
console.log(str2.trimEnd());
console.log(str3.trim());
console.log(str4.trim());

Output:

"hello world"
"hello world"
"hello world"
"hello world"

We can use the following code by getting advantage of String.prototype if we don’t want to use the built-in trim() function. It performs the same as the trim() function does but uses the replace() method with regex to accomplish the goal.

String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g, '');
};

String.prototype.lefttrim = function() {
  return this.replace(/^\s+/, '');
};

String.prototype.righttrim = function() {
  return this.replace(/\s+$/, '');
};

var text = '    hello world  ';
console.log(text.lefttrim());
console.log(text.righttrim());
console.log(text.trim());

Output:

"hello world  "
"    hello world"
"hello world"

Here is the solution for replacing all the white spaces. This approach is very useful to remove spaces in contact numbers.

var text = '    +92 345 254 2769      ';
console.log(text.replace(/\s/g, ''));

Output:

"+923452542769"

Use jQuery to Simulate Python stripe() Method in JavaScript

var str1 = ' hello world';
var str2 = 'hello world ';
var str3 = ' hello world ';
var str4 = '           hello world ';
console.log($.trim(str1));
console.log($.trim(str2));
console.log($.trim(str3));
console.log($.trim(str4));

Output:

"hello world"
"hello world"
"hello world"
"hello world"
Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook