修剪字符串中的特定字符 [英] Trim specific character from a string
本文介绍了修剪字符串中的特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是 JavaScript 等效于此 C#
方法:
var x = "|f|oo||";
var y = x.Trim('|'); // "f|oo"
C#仅在开头修剪所选字符字符串的strong>和结束!
推荐答案
一行就足够了:
var x = '|f|oo||';
var y = x.replace(/^\|+|\|+$/g, '');
document.write(x + '<br />' + y);
^\|+ beginning of the string, pipe, one or more times
| or
\|+$ pipe, one or more times, end of the string
在函数 中:
In a function :
function trim (s, c) {
if (c === "]") c = "\\]";
if (c === "\\") c = "\\\\";
return s.replace(new RegExp(
"^[" + c + "]+|[" + c + "]+$", "g"
), "");
}
s = ".foo..oo...";
console.log(s, "->", trim(s, "."));
s = "|foo||oo|||";
console.log(s, "->", trim(s, "|"));
s = "]foo]]oo]]]";
console.log(s, "->", trim(s, "]"));
s = "\\foo\\\\oo\\\\\\";
console.log(s, "->", trim(s, "\\"));
这篇关于修剪字符串中的特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文