js中的string对象

来源:互联网 发布:网络监察电话是多少 编辑:程序博客网 时间:2024/05/21 17:56
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title></head><!--        描述:JS是基于对象的,JS也提供了一些常用的对象。        Java面向对象:class {构造器}    JS基于对象:构造函数        String     创建字符串对象    var str = new String("aaa");    比较两个字符串的内容    str1.valueOf() === str2.valueOf()    取出对应下表的字符    str.charAt(index);    取出对应下标的字符的asci码        str.charCodeAt(indext)    得到字符串的长度    str.length    字符串的连接    str.concat(str1,str2...);    返回子字符串在字符串中第一次出现的下标        str.indexOf("love"); //返回love 在 str中第一次出现的位置    返回子字符串在字符串中最后一次出现的下标        str.lastIndexOf("love"); //返回love 在 str中最后一次出现的位置    分割字符串    str.split("seperator");  将str根据seperator分割    截取字符串    str.substring(start,end); 从start截取到end    将字符串转成大写        str.toUpperCase();    将字符串转成小写    str.toLowerCase();            字符串替换            str.replace(正则表达式,"替换的字符串");            str.replace(/love/,"LOVE"); //只替换一个            str.replace(/love/g,"LOVE"); //全局替换        给字符串两端加一个<font></font>,并添加color属性    str.fontcolor("颜色");    str.fontcolor("#FF00FF");    str.fontcolor("red");    str.fontcolor("rgb(200,30,100)");    //<font color="rgb(255,200,200)">aaa</font>    把 HTML <I> 标记放置在 String 对象中的文本两端。     str.italics(); //斜体    //<i>The first love, the last love!</i>                        把一个有 HREF 属性的 HTML 锚点放置在 String 对象中的文本两端。str3.link("http://www.baidu.com"); //<a href="http://www.baidu.com">The first love, the last love!</a>                Number    Date    Math(类)        --><body><script type="text/javascript">var str1 = new String("aaa");var str2 = new String("bbb");//alert(str1 == str2); //false//比较两个字符串对象的内容是否相同//首先需要取得字符串对象的值 str.valueOf()//alert(str1.valueOf() === str2.valueOf());  //truefor(var i = 0; i < str1.length; i++) {document.write(str1.charCodeAt(i));document.write("<br/>")}document.write(str1.concat(str2, "ccc", "ddd", "<br/>"));//str1两边加一个<font>aaa</font>//给font标签添加一个color属性:属性值是传入参数。document.write(str1.fontcolor("rgb(255,200,200)"));document.write("<br/>")var str3 = new String("The first love, the last love!");var index = str3.indexOf("love");document.write("love".fontcolor("red") + "出现的位置是:" + index+"<br/>");var arr = str3.split(" ");for(var index in arr){document.write(arr[index]+"<br/>");}alert(str3.substring(10,25));alert(str3.toUpperCase());document.write(str3.italics());document.write(str3.link("http://www.baidu.com"));var str4 = str3.replace(/love/g,"shit");document.write(str4);</script></body></html>

0 0