Javascript:切片和子字符串未按预期工作
我希望从第二个位置给我4个字符。但事实并非如此Javascript:切片和子字符串未按预期工作,javascript,string,Javascript,String,我希望从第二个位置给我4个字符。但事实并非如此 <body id = "A"> <script> var s = document.getElementById("A"); var d = "Hello, world"; s.innerHTML = d.substring(2,4); </script> 子字符串的参数是start和end,而不是start和length string.substring(start,end) Parameter Values
<body id = "A">
<script>
var s = document.getElementById("A");
var d = "Hello, world";
s.innerHTML = d.substring(2,4);
</script>
子字符串的参数是start和end,而不是start和length
string.substring(start,end)
Parameter Values
Parameter Description
start Required. The position where to start the extraction. First character is at index 0
end Optional. The position (up to, but not including) where to end the extraction. If omitted, it extracts the rest of the string
有一个加法函数substr,其参数为start和length
string.substr(start,length)
Parameter Values
Parameter Description
start Required. The position where to start the extraction. First character is at index 0
length Optional. The number of characters to extract. If omitted, it extracts the rest of the string
你可以。。您可能会发现这是您遇到的其他问题的有用参考。您需要
而且是不同的方法 你可能想要substr。
string.substr(start,length)
Parameter Values
Parameter Description
start Required. The position where to start the extraction. First character is at index 0
length Optional. The number of characters to extract. If omitted, it extracts the rest of the string
var s = document.getElementById("A");
var d = "Hello, world";
s.innerHTML = d.trim().substr(2,4);