//字符中的字符从左向右进行索引,由0开始,字符串中的空格也算在内
var string = "charAt find word position"; document.write("charAt find word position"+ "<br/>") document.write("string.charAt(0) is:" + string.charAt(0) + "<br/>"); document.write("string.charAt(1) is:" + string.charAt(1) + "<br/>"); document.write("string.charAt(2) is:" + string.charAt(2) + "<br/>"); document.write("string.charAt(3) is: " + string.charAt(3) + "<br/>"); document.write("string.charAt(4) is: " + string.charAt(4) + "<br/>"); document.write("string.charAt(5) is: " + string.charAt(5) + "<br/>"); document.write("string.charAt(6) is: " + string.charAt(6) + "<br/>"); document.write("string.charAt(7) is: " + string.charAt(7) + "<br/>"); document.write("string.charAt(string.length-2) is: " + string.charAt(string.length-2) + "<br/>") document.write("string.charAt(string.length-1) is: " + string.charAt(string.length-1) + "<br/>")
//输出结果
string.charAt(0) is:c
string.charAt(1) is:h
string.charAt(2) is:a
string.charAt(3) is: r
string.charAt(4) is: A
string.charAt(5) is: t
string.charAt(6) is:
string.charAt(7) is: f
string.charAt(string.length-2) is: o
string.charAt(string.length-1) is: n
//返回该字符串
function findCharAt(str,n){ var obj = String(str); // 转化为字符串 var pos; //定义变量 pos = obj.charAt(n-1); //查找的索引 return pos; //返回字符 } alert(findCharAt("这是第几个",1));