替换字符串的第一个字符 [英] Replace first character of string
本文介绍了替换字符串的第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串 | 0 | 0 | 0 | 0
但它必须 0 | 0 | 0 | 0
如何替换第一个字符('|'
)(''
)。例如替换('|','')
How do I replace the first character ('|'
) with (''
). eg replace('|','')
(使用JavaScript)
(with JavaScript)
推荐答案
你可以完全拥有你所拥有的:)
You can do exactly what you have :)
var string = "|0|0|0|0";
var newString = string.replace('|','');
alert(newString); // 0|0|0|0
你可以看到它在这里工作, .replace()
在javascript中默认只替换第一次出现(没有 / g
),所以这对你有利:)
You can see it working here, .replace()
in javascript only replaces the first occurrence by default (without /g
), so this works to your advantage :)
如果你需要检查第一个字符是否是pipe:
If you need to check if the first character is a pipe:
var string = "|0|0|0|0";
var newString = string.indexOf('|') == 0 ? string.substring(1) : string;
alert(newString); // 0|0|0|0
这篇关于替换字符串的第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文