如何删除(或隐藏)字符串中的字符,例如: 我有20个字符的字符串,我需要删除15中的所有字符。 附:我有一个想法,但不要认为它是正确的:用不同的类创建两个段落,一个段落包含15个字符,第二个段落不重要并删除/隐藏第二段 例如:
var count;
count = $('#mydiv').text().length;
if (count => 15) {
}
答案 0 :(得分:1)
您正在寻找substring()方法。
var text = "this is a super long string.";
var shortText = text.substring(0, 15);
// shortText now is "this is a super"
答案 1 :(得分:0)
使用string.substr
var str = "Hello world!";
var res = str.substr(0, 5); // returns Hello
答案 2 :(得分:0)
使用slice
功能 - string.slice(start, end)
var res = str.slice(0, 16);
答案 3 :(得分:0)
https://css-tricks.com/snippets/css/truncate-string-with-ellipsis/
.truncate {
width: 250px; // or any other width
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
_.truncate('hi-diddly-ho there, neighborino');
// => 'hi-diddly-ho there, neighbo...'
_.truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator': ' '
});
// => 'hi-diddly-ho there,...'
_.truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator': /,? +/
});
// => 'hi-diddly-ho there...'
_.truncate('hi-diddly-ho there, neighborino', {
'omission': ' [...]'
});
// => 'hi-diddly-ho there, neig [...]'
答案 4 :(得分:0)
如果您知道要删除的内容,则只需将其替换为.replace(),不确定隐藏字符的确切含义。尽管https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
,所有答案似乎都是您问题的可能解决方案答案 5 :(得分:0)
我建议你看一下子串支持页面:
https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/String/substring
jsfiddle with substring use:
https://jsfiddle.net/zdLdk6Lj/
var divtext = $('#mydiv').text();
$('#mydiv').text(divtext.substring(0,15));