Javascript javascript中的RTrim

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8791394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:31:24  来源:igfitidea点击:

RTrim in javascript

javascript

提问by Nate Pet

I am having a problem with jQuery's trim. I have a string such at in jQuery:

我在使用 jQuery 的修剪时遇到了问题。我在 jQuery 中有一个字符串:

var string1;
string1 = "one~two~";

How do I trim the trailing tilde?

如何修剪尾随波浪号?

回答by Gabriele Petrioli

The .trim()method of jQuery refers to whitespace ..

.trim()jQuery的方法是指空格..

Description: Remove the whitespace from the beginning and end of a string.

描述:删除字符串开头和结尾的空格。



You need

你需要

string1.replace(/~+$/,'');

This will remove all trailing ~.

这将删除所有尾随~.

So one~two~~~~~would also become one~two

所以one~two~~~~~也会变成one~two

回答by Konerak

Just use the javascript replace to change the last string into nothing:

只需使用 javascript 替换将最后一个字符串更改为空:

string1.replace(/~+$/g,"");

回答by Javid

IMO this is the best way to do a right/left trim and therefore, having a full functionality for trimming (since javascript supports string.trimnatively)

IMO 这是进行右/左修剪的最佳方式,因此具有完整的修剪功能(因为 javascriptstring.trim本身支持)

String.prototype.rtrim = function (s) {
    if (s == undefined)
        s = '\s';
    return this.replace(new RegExp("[" + s + "]*$"), '');
};
String.prototype.ltrim = function (s) {
    if (s == undefined)
        s = '\s';
    return this.replace(new RegExp("^[" + s + "]*"), '');
};

Usage example:

用法示例:

var str1 = '   jav ~'
var r1 = mystring.rtrim('~'); // result = '   jav ' <= what OP is requesting
var r2 = mystring.rtrim(' ~'); // result = '   jav'
var r3 = mystring.ltrim();      // result = 'jav ~'

P.S.If you are specifying a parameter for rtrimor ltrim, make sure you use a regex-compatible string. For example if you want to do a rtrimby [, you should use: somestring.rtrim('\\[')If you don't want to escape the string manually, you can do it using a regex-escape function if you will. See the answer here.

PS如果您为rtrimor指定参数ltrim,请确保使用与正则表达式兼容的字符串。例如,如果您想执行rtrimby [,则应使用:somestring.rtrim('\\[')如果您不想手动转义字符串,则可以使用 regex-escape 函数(如果愿意)。请参阅此处的答案

回答by Silvertiger

One option:

一种选择:

string1 = string1.substring(0,(string1.length-1));

long way around it .. and it jsut strips the last character .. not the tilde specifically..

绕着它走了很长一段路..它只是去掉了最后一个字符..不是特别的波浪号..

回答by Nick

var myStr = "One~Two~Three~Four~"     
var strLen = myStr.length;
myStr = myStr.slice(0,strLen-1);
alert (myStr);

This will delete the last character in the string. Is that what you wanted?

这将删除字符串中的最后一个字符。那是你想要的吗?

回答by ShankarSangoli

You can use substringjavascript method.

您可以使用substringjavascript 方法。

Try this

尝试这个

var string1 = "one~two~";
string1 = $.trim(string1).substring(0, string1.length -1);