js基本功——String
目录:
1,constructor,构造函数
2,length,程度
3,prototype,原型
anchor() 以锚的方式显示
document.write("hello world".anchor("hi"));
//<a name = "hi">hello world</a>
link() 以链接的方式显示
document.write("anch".link("http://anch.site"));
//<a href="http://anch.site">anch</a>
big() small()
document.write("hello world<br>");
document.write("hello world".big());
bold() italics() strike() sub() sup()
document.write("hello".bold());
document.write("hello".italics());
document.write("hello".strike());
charAt(index)
document.write("你好 世界".charAt(0));
charCodeAt(index)
document.write("你好 世界".charCodeAt(0));
concat(str1,str2,strn) 连接字符串
document.write("hello".concat(" w","o","rld"));
split(separator) 分割字符串为数组
document.write("hello world happy".split(" "));
fixed()
document.write("hello");
document.write("hello".fixed());
fontcolor(color) 亲测该方法对rgb表达式支持有问题
document.write("hello".fontcolor("red"));
document.write("hello".fontcolor("#0f0"));
fontsize(size) size为1至7的整数,其中3为正常大小
document.write("hello".fontsize(3));
String.fromCharCode(num1,num2,...) num为Unicode编码
document.write(String.fromCharCode(72,69,76,76,79));
indexOf(substr) 第一个字符的index是0
str ="Hello World";
document.write(str.indexOf("World"));
lastIndexOf(substr)
localeCompare(str) 按照语言自己的排序方法进行排序比较,小则返回负数,大则返回整数,相等返回0
str="陈";
document.write("安".localeCompare(str));
match(substr) 检索指定值或匹配到的正则表达式的多个值
document.write("hello".match("l"));
正则表达式请参阅正则表达式部分
replace() search()
正则表达式请参阅正则表达式部分
slice(startIndex, endIndex) 通过下标截取字符串,start和end各自为正从头开始定位,为负数则从尾开始定位
document.write("hello world happy".slice(6,11));
substr(startIndex, length) 通过起始位置和长度截取字符串,start为正从头开始定位,为负数则从尾开始定位
document.write("hello world happy".substr(6,5));
substring() 同slice,但不支持负数参数
toLowerCase(str) toUpperCase()转换为小写,大写
toLocaleLowerCase(str) toLocaleUpperCase()转换为小写,大写,较toLowerCase,toUpperCase严谨,支持其它字母型文字
toString() 返回对象的字符串形式
valueOf() 返回字符串的原始值,通常为系统隐式调用
/*字符串内置函数练习*/ var str1 = "hello world <br>"; var str2 = "Hello worlD <br>"; var s1 = "hel"; var s2 = "lo"; var s3 = " world"; var d=document; // 功能标签 d.write("<h1>1 功能标签</h1>"); d.write(str1.anchor("str1")+"<br>"); d.write(str1.link("#str1")); // 显示样式 d.write("<h1>2 显示样式</h1>"); d.write(str1.big()); d.write(str1.small()); d.write(str1.italics()); d.write(str1.bold()); d.write(str1.fontsize(7)); d.write(str1.fontcolor("#f0f")); d.write(str1.strike()); d.write("100"+"2".sup()+"<br>"); d.write("CO"+"2".sub()+"<br>"); d.write(str1.fixed()); // 大小写转换 d.write("<h1>3 大小写转换</h1>"); d.write(str2.toLowerCase()); d.write(str2.toLocaleLowerCase()); d.write(str2.toUpperCase()); d.write(str2.toLocaleUpperCase()); // 截取字符串 d.write("<h1>4 截取字符串</h1>"); d.write(str1.substr(2,5)+"<br>"); d.write(str1.substr(-5,2)+"<br>"); d.write(str1.slice(2,5)+"<br>"); d.write(str1.slice(-10,-5)+"<br>"); d.write(str1.substring(2,3)+"<br>"); // 查询 d.write("<h1>5 查询</h1>"); d.write(str1.charAt(0)+"<br>"); d.write(str1.indexOf("wo")+"<br>"); d.write(str1.lastIndexOf('wo')+"<br>"); // 拆分字符串 d.write("<h1>6 拆分字符串</h1>"); d.write(str1.split(" ")); // 字符串连接 d.write("<h1>7 字符串连接</h1>"); d.write(s1.concat(s2,s3)); // 编码相关 d.write("<h1>8 编码相关</h1>"); d.write(str1.charCodeAt(0)+"<br>"); d.write(String.fromCharCode(104)+"<br>");

浙公网安备 33010602011771号