search
HomeWeb Front-endJS Tutorial5 implementation methods and comparisons of js removing spaces before and after strings_javascript skills

If we write in the project that when the user enters spaces during registration, how do we remove the spaces?
The following is the js I often use to share with you:

The first type: Loop check and replace
[javascript]

Copy code The code is as follows:

//For users to call
function trim(s){
return trimRight(trimLeft(s));
}
//Remove the left blank
function trimLeft( s){
if(s == null) {
return "";
}
var whitespace = new String(" tnr");
var str = new String(s) ;
if (whitespace.indexOf(str.charAt(0)) != -1) {
var j=0, i = str.length;
while (j j ;
}
str = str.substring(j, i);
}
return str;
}
//Remove the whitespace on the right www.jb51.net
function trimRight(s){
if(s == null) return "";
var whitespace = new String(" tnr" );
var str = new String(s);
if (whitespace.indexOf(str.charAt(str.length-1)) != -1){
var i = str.length - 1;
while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1){
i--;
}
str = str.substring( 0, i 1);
}
return str;
}

Second: regular replacement
[javascript]
Copy code The code is as follows:



Third method: use jquery
[javascript]
Copy code The code is as follows:

$.trim(str)

The internal implementation of jquery is:
[javascript]
Copy code The code is as follows:

function trim(str){
return str.replace(/^(s|u00A0) / ,'').replace(/(s|u00A0) $/,'');
}

The fourth way: use motorcycles
[javascript ]
Copy code The code is as follows:

function trim(str){
return str .replace(/^(s|xA0) |(s|xA0) $/g, '');
}

The fifth way: cutting strings
[javascript]
Copy code The code is as follows:

function trim(str) {
str = str.replace(/^(s|u00A0) /,'');
for(var i=str.length-1; i>=0; i--){
if(/S/.test(str.charAt(i))){
str = str.substring(0, i 1);
break;
}
}
return str ;
}

After testing, the fifth method is the most efficient when processing long strings.
Copy code The code is as follows:

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools