Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:切片和子字符串未按预期工作_Javascript_String - Fatal编程技术网

Javascript:切片和子字符串未按预期工作

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

我希望从第二个位置给我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
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);