简体   繁体   English

Javascript - 如何删除字符串开头的空格

[英]Javascript - How to remove the white space at the start of the string

我想删除字符串开头的空格它应该只删除字符串开头的空格,其他空格应该在那里。

var string=' This is test';

This is what you want:这就是你想要的:

function ltrim(str) {
  if(!str) return str;
  return str.replace(/^\s+/g, '');
}

Also for ordinary trim in IE8+:也适用于 IE8+ 中的普通修剪:

function trimStr(str) {
  if(!str) return str;
  return str.replace(/^\s+|\s+$/g, '');
}

And for trimming the right side:并用于修剪右侧:

function rtrim(str) {
  if(!str) return str;
  return str.replace(/\s+$/g, '');
}

Or as polyfill:或者作为 polyfill:

// for IE8
if (!String.prototype.trim)
{
    String.prototype.trim = function ()
    {
        // return this.replace(/^\s+|\s+$/g, '');
        return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
    };
}

if (!String.prototype.trimStart)
{
    String.prototype.trimStart = function ()
    {
        // return this.replace(/^\s+/g, '');
        return this.replace(/^[\s\uFEFF\xA0]+/g, '');
    };
}

if (!String.prototype.trimEnd)
{
    String.prototype.trimEnd = function ()
    {
        // return this.replace(/\s+$/g, '');
        return this.replace(/[\s\uFEFF\xA0]+$/g, '');
    };
}

Note:笔记:
\\s: includes spaces, tabs \\t, newlines \\n and few other rare characters, such as \\v, \\f and \\r. \\s:包括空格、制表符\\t、换行符\\n 和一些其他稀有字符,例如\\v、\\f 和\\r。
\: Unicode Character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF) \:Unicode 字符“零宽度无间断空间”(U+FEFF)
\\xA0: ASCII 0xA0 (160: non-breaking space) is not recognised as a space character \\xA0:ASCII 0xA0(160:不间断空格)不被识别为空格字符

Try to use javascript's trim() function, Basically it will remove the leading and trailing spaces from a string.尝试使用 javascript 的trim()函数,基本上它会从字符串中删除前导和尾随空格。

var string=' This is test';
string = string.trim();

DEMO演示

So as per the conversation happened in the comment area, in order to attain the backward browser compatibility just use jquery's $.trim(str)因此,根据评论区中发生的对话,为了实现向后浏览器兼容性,只需使用 jquery 的$.trim(str)

var string=' This is test';    
string = $.trim(string)

You can use String.prototype.trimStart().您可以使用 String.prototype.trimStart()。 Like this:像这样:

myString=myString.trimStart();

An if you want to trim the tail, you can use:如果你想修剪尾巴,你可以使用:

myString=myString.trimEnd();

Notice, this 2 functions are not supported on IE.请注意,IE 不支持这 2 个功能。 For IE you need to use polyfills for them.对于 IE,您需要为它们使用 polyfill。

You should use javascript trim function您应该使用javascript 修剪功能

var str = "       Hello World!        ";
alert(str.trim()); 

This function can also remove white spaces from the end of the string.此函数还可以从字符串的末尾删除空格。

.trimLeft() can be used for this. .trimLeft()可用于此目的。

const str = "   string   ";
console.log(str.trimLeft());     // => "string   "

Just a different and a not so efficient way to do it只是一种不同且不太有效的方式来做到这一点

 var str = " My string"; function trim() { var stringStarted = false; var newString = ""; for (var i in str) { if (!stringStarted && str[i] == " ") { continue; } else if (!stringStarted) { stringStarted = true; } newString += str[i]; } return newString; } console.log(trim(str));

I am really sure this doesn't work for anything else and is not the most optimum solution, but this just popped into my mind我真的很确定这对其他任何事情都不起作用,也不是最理想的解决方案,但这只是我想到的

Easiest solution: (ES10 feature trimStart())最简单的解决方案:(ES10 特性trimStart())

var string= 'This is a test';
console.log(string.trimStart());
var string=' This is test';    
$.trim(string);

You want to remove the space (ie whitespace) at the beginning of the string.您想删除字符串开头的空格(即空格)。 So, could not use standard jquesy .trim() .所以,不能使用标准的 jquesy .trim() You can use the regex to find and replace the whitespace at the beginning of the string.您可以使用正则表达式查找并替换字符串开头的空格。

Try this:尝试这个:

.replace(/^\s+/g, "")

Read this post 阅读这篇文章

Try this:尝试这个:

var string='    This is test   ';
alert(string.replace(/^\s+/g, ""));

Working Example工作示例

OR if you want to remove the whitespace from the beginning and end then use .trim()或者,如果您想从开头和结尾删除空格,请使用.trim()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM