[英]remove all characters of a string after 5th character?
例如,我需要刪除字符串中第5個字符后的所有字符
我的繩子是
P-CI-7-C-71
和輸出應該像
P-CI-
提取弦樂部分
有三種方法可以提取字符串的一部分:
slice(start, end)
substring(start, end)
substr(start, length)
使用String#substring
:
substring()
方法返回一個索引與另一個索引之間或字符串末尾的字符串子集。
document.write('P-CI-7-C-71'.substring(0, 5));
嘗試這個:
alert('P-CI-7-C-71'.substring(0,5));
var str = 'P-CI-7-C-71'; var res = str.slice(0,5); alert(res);
使用可以在JS中使用子字符串功能
Substring(StringName,StartPosition,End Position);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.