Javascript 学习笔记之String类测试
/* -------------------------
String类 测试
----------------------------*/
function writeline(message)
{
document.write(message);
document.write("<br>");
}
var str1 = "hello world";
var str2 = new String("hello world");
writeline(str1 == str2) //true;
writeline(str1.length);//11 length返回字符串中字符的个数
writeline(str1.charAt(1)); //e
writeline(str1.charCodeAt(1)); //101 e的编码
writeline(str1.concat(" hi"));//hello world hi
writeline(str1 + " hi"); //hello world hi
writeline(str1.indexOf("h")); //0
writeline(str1.lastIndexOf("l")); //9
writeline(str1.localeCompare("a")); //1
writeline(str1.localeCompare(str2));//0
writeline(str1.localeCompare("z"));//-1
writeline(str1.slice(3)); //lo world
writeline(str1.substring(3)); //lo world
writeline(str1.slice(3,7)); //lo w
writeline(str1.substring(3,7)); //lo w
writeline(str1.slice(-3));//rld 取最后三个字符
writeline(str1.substring(-3)); //hello world 忽略负数,
writeline(str1.slice(3,-4));//lo w slice(3,7)
writeline(str1.slice(3,-4));//lo w substring(0,3)
writeline(str1.toUpperCase())
writeline(str1.toLocaleUpperCase());
writeline(str1.toLowerCase());
writeline(str1.toLocaleLowerCase());
<待续>
String类 测试
----------------------------*/
function writeline(message)
{
document.write(message);
document.write("<br>");
}
var str1 = "hello world";
var str2 = new String("hello world");
writeline(str1 == str2) //true;
writeline(str1.length);//11 length返回字符串中字符的个数
writeline(str1.charAt(1)); //e
writeline(str1.charCodeAt(1)); //101 e的编码
writeline(str1.concat(" hi"));//hello world hi
writeline(str1 + " hi"); //hello world hi
writeline(str1.indexOf("h")); //0
writeline(str1.lastIndexOf("l")); //9
writeline(str1.localeCompare("a")); //1
writeline(str1.localeCompare(str2));//0
writeline(str1.localeCompare("z"));//-1
writeline(str1.slice(3)); //lo world
writeline(str1.substring(3)); //lo world
writeline(str1.slice(3,7)); //lo w
writeline(str1.substring(3,7)); //lo w
writeline(str1.slice(-3));//rld 取最后三个字符
writeline(str1.substring(-3)); //hello world 忽略负数,
writeline(str1.slice(3,-4));//lo w slice(3,7)
writeline(str1.slice(3,-4));//lo w substring(0,3)
writeline(str1.toUpperCase())
writeline(str1.toLocaleUpperCase());
writeline(str1.toLowerCase());
writeline(str1.toLocaleLowerCase());