js字符串基本操作

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Sup_</title>
<style>
</style>
<script type="text/javascript">
window.onload=function(){
//-----------------------------定义字符串
var txt= 'Sup_';
//alert(txt);//----------------------输出:Sup_

//-----------------------------length:字符串长度
//alert(txt.length);//---------------输出:4

//-----------------------------charAt:返回指定位置的字符
//alert(txt.charAt(2));//------------输出:p

//-----------------------------charCodeAt:转为Unicode
//alert(txt.charCodeAt(2));//---------输出:112

//-----------------------------concat:连接字符串
//alert(txt.concat(txt));//-----------输出Sup_Sup_

//-----------------------------fromCharCode:将Unicode转为字符
//alert(String.fromCharCode(112));//-----输出:p

//-----------------------------indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置
//alert(txt.indexOf('p'));//-------------输出:2

//-----------------------------lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
//alert(txt.lastIndexOf('p'));//-----------输出:2

//-----------------------------match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。如果没有找到任何匹配的文本, match() 将返回 null。否则,它将返回一个数组,其中存放了与它找到的匹配文本有关的信息。
//alert(txt.match(/s/i));//------------------输出:S

//----------------------------replace()用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
//alert(txt.replace(/S/,'s'));//------------输出sup_

//----------------------------search()search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。返回值为序号,如果没有找到任何匹配的子串,则返回 -1。
//alert(txt.search(/S/i));

//----------------------------slice()提取字符串段,参数为起始位置和结束位置
//alert(txt.slice(1,3));//--------------提取1-3位;输出up

//----------------------------substr()提取字符串段,参数为起始位置和长度
//alert(txt.substr(1,3));//-------------输出:up_

//----------------------------split()切分字符串;以传入的参数为切分段
//alert(txt.split('u'));//-----------输出:S,pe_

//----------------------------toLowerCase()转为小写
//alert(txt.toLowerCase());//----------输出sup_
//----------------------------toUpperCase()转为大写
//alert(txt.toUpperCase());//------------输出SUP_
};
</script>
</head>
<body>
</body>
<html>

posted @ 2017-01-08 10:56    阅读(336)  评论(0编辑  收藏  举报