打字稿:trim 特定字符数后的其余文本
typescript: trim the rest of the text after a specific number of characters
我想将文本限制为 15 个字符,如果超出,其余文本应 ...
你是怎么做到的?
我正在使用这个
return txt.substr(15, txt.length);
但是,它删除了前 15 个字符
if(txt.length >= 15) {
txt = txt.substring(0, 15) + '...';
}
或者,如果您仍然只想显示 15 个字符:
if(txt.length >= 15) {
txt = txt.substring(0, 12) + '...';
}
您也可以使用 concat 函数。
if(txt.length >= 15) {
return txt.substr(0,15).concat('...');
} else {
return txt;
}
下面的 typescript
代码对我有用。,
let txt = '1234567890FIFTH_REPLACEME';
return txt.slice(0, 15).concat('...');
JavaScript中的工作示例:
单击下面的 运行 代码片段按钮,然后单击 "Try It" 按钮查看结果。,
function myFunction() {
var str = "1234567890FIFTH_REPLACEME";
var res = str.slice(0, 15).concat('...');
document.getElementById("demo").innerHTML = res;
return res;
}
<p>Click the button to display the extracted part of the string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
我想将文本限制为 15 个字符,如果超出,其余文本应 ...
你是怎么做到的?
我正在使用这个
return txt.substr(15, txt.length);
但是,它删除了前 15 个字符
if(txt.length >= 15) {
txt = txt.substring(0, 15) + '...';
}
或者,如果您仍然只想显示 15 个字符:
if(txt.length >= 15) {
txt = txt.substring(0, 12) + '...';
}
您也可以使用 concat 函数。
if(txt.length >= 15) {
return txt.substr(0,15).concat('...');
} else {
return txt;
}
下面的 typescript
代码对我有用。,
let txt = '1234567890FIFTH_REPLACEME';
return txt.slice(0, 15).concat('...');
JavaScript中的工作示例:
单击下面的 运行 代码片段按钮,然后单击 "Try It" 按钮查看结果。,
function myFunction() {
var str = "1234567890FIFTH_REPLACEME";
var res = str.slice(0, 15).concat('...');
document.getElementById("demo").innerHTML = res;
return res;
}
<p>Click the button to display the extracted part of the string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>