首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

javascript基础(5)JavaScript中的字符串对象

2012-08-30 
javascript基础(五)JavaScript中的字符串对象今天复习下javascript的字符串对象 其中包括字符串的创建,字

javascript基础(五)JavaScript中的字符串对象
今天复习下javascript的字符串对象 其中包括字符串的创建,字符串的长度,charAt ?charCodeAt方法 indexOf lastIndexOf slice方法 ?new String(s)返回一个新建的String对象,存放的是字符串s或者s的字符串表示。String(s)把s转换成原始的字符串,并返回转换后的值
字符串的长度??字符串中的length属性表示字符串的长度length属性的返回值,是字符串中的字符数。
???
charAt方法
返回字符串中指定位置的字符。?charCodeAt方法
返回指定位置的字符的Unicode编码。这个返回值是0到65535之间的整数。?concat方法
用于连接两个或多个字符串。?indexOf方法
返回某个指定的字符串值在字符串中首次出现的位置。?lastIndexOf方法
返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。?slice方法
提取字符串的片断,并在新的字符串中返回被提取的部分。?substring方法?split方法
把字符串分割为字符串数组。下面看实例代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD>  <TITLE> JavaScript中的字符串对象 </TITLE>  <META NAME="Content-Type" CONTENT="text/html;charset=utf-8">  <META NAME="Author" CONTENT="">  <META NAME="Keywords" CONTENT="">  <META NAME="Description" CONTENT="">  <SCRIPT LANGUAGE="JavaScript">  <!--//字符串的两种创建方式document.write("new String(s)方式创建字符串对象<br/>");var str=new String("Hello world!");document.write("创建的字符串为:"+str+"<br/>");document.write("String(s)方式创建字符串对象<br/>");str=String("LangSin JavaScript framework!");document.write("创建的字符串为:"+str+"<br/>");//字符串的length属性var charnum=str.length;document.write(str+"的长度为:"+charnum+"<br/>");//字符串的charAt方法var c=str.charAt(8);document.write("<br/>");document.write("字符串的charAt方法演示<br/>");document.write(str+"的第九个字符为:"+c+"<br/>");//字符串的charCodeAt方法//大写字母的unicode范围在65——90之间//小写字母的unicode范围在97——122之间var cc=str.charCodeAt(1);document.write("<br/>");document.write("字符串的charCodeAt方法演示<br/>");document.write(str+"的第一个字符的unicode编码为:"+cc+"<br/>");//字符串的cancat方法document.write("<br/>");document.write("字符串的cancat方法演示<br/>");var str1="abcdefg";var str2="_-abc";var newstr=str.concat(str1,str2);document.write("新的字符串为:"+newstr+"<br/>");//字符串的indexOf方法document.write("<br/>");document.write("字符串的indexOf方法演示<br/>");var index=newstr.indexOf(str2);document.write(newstr+"中第一次出现'"+str2+"'的位置索引为:"+index+"<br/>");//字符串的lastIndexOf方法document.write("<br/>");document.write("字符串的lastIndexOf方法演示<br/>");index=newstr.lastIndexOf("abc");document.write(newstr+"中最后一次出现'abc'的位置索引为:"+index+"<br/>");if(newstr.length-3==index){//alert("成功!");}//字符串的slice方法//在slice方法中,如果参数为负数,则会把该负数转换为源字符串的总长度加上该负数的值document.write("<br/>");document.write("字符串的slice方法演示<br/>");var result=newstr.slice(str.length,-2);document.write("截取的字符串结果为:"+result+"<br/>");//字符串的substring方法//该方法与slice方法类似,不同的是substring会把参数中的负数转换为0document.write("<br/>");document.write("字符串的substring方法演示<br/>");result=newstr.substring(str.length,-2);document.write("截取的字符串结果为:"+result+"<br/>");//字符串的split方法document.write("<br/>");document.write("字符串的split方法演示<br/>");var arrstr=newstr.split("_");for(var i=0;i<arrstr.length;i++){document.write(i+":"+arrstr[i]+"<br/>");}  //-->  </SCRIPT> </HEAD> <BODY>   </BODY></HTML>
?

?

?

?

热点排行
Bad Request.