javascript学习笔记__JS字符串用法

JS字符串用法

字符串
    有两种形式的字符串:基本类型,对象类型  
    对象类型的字符串封装了一些属性及方法,而基本类型则没有
    可以直接用基本类型字符串调用字符串对象封装的属性及方法,无须手动转换。

<script type="text/javascript">			
	var str1 = "Hello World";
	console.log( typeof str1 );				//string
	var str2 = new String("Hello World");
	console.log( typeof str2 );				//object
</script>

字符串的长度
    字符串中的length是只读属性,不可更改     str.length
    按各国标注字符来计算长度 
字符串的常用方法
    charAt(index)
        功能
            获取对应下标处的字符
        参数
            下标
        返回值
            获取到的字符,没有对应下标则返回空字符串

<script type="text/javascript">			
	var str = "You are so beautiful!";
	console.log( str.charAt(4) );				//a
	console.log( str.charAt(40) );				//空字符串
	console.log( typeof str.charAt(40) );		//string			
</script>

  charCodeAt(index)
        功能
            获取对应下标处的字符编码
        参数
            下标
        返回值
            获取到的字符编码(0-65535),没有对应的下标则返回NaN

<script type="text/javascript">			
	var str = "You are so beautiful!";
	//获取对应下标处的字符编码
	console.log( str.charCodeAt(4) );				//97
	console.log( str.charCodeAt(40) );				//NaN
	console.log( typeof str.charCodeAt(4) );		//number			
</script>

  String.fromCharCode(ASCII码)
        功能
            获取ASCII码对应的字符
        参数
            ASCII码
        返回值
            对应的字符

<script type="text/javascript">			
	//参数:ASCII码
	//返回值:对应的字符
	var charStr = String.fromCharCode(117);
	console.log( charStr );				//u
</script>

 toLowerCase(), toUpperCase()
        功能    
            字符串的大小写转换
        返回值
            转换后的字符串
        注意:不会改变原字符串

<script type="text/javascript">			
	var str = "Hello";
	//全部转换为大写
	console.log( str.toUpperCase() );	//HELLO
	//不会更改原字符串
	console.log( str );					//Hello
	//全部转换为小写
	console.log( str.toLowerCase() );	//hello
	//不会更改原字符串
	console.log( str );					//Hello
</script>

==, ===判断相等
        ==
            只比较值相等,不对类型进行比较(比较之前会先进行类型统一)
        ===
            要求值和类型均相等

<script type="text/javascript">			
	console.log( 1 == "1" );		//true
	console.log( 1 == true );		//true
	console.log( 2 == true );		//false
	console.log( 0 == true );		//false
	console.log( 2 == false );		//false
	console.log( 0 == false );		//true
			
	console.log( 1 === "1" );		//false
	console.log( 1 === true );		//false
</script>

localeCompare()
        字符串比较大小
        规则
            两个字符串从下标为0的字符开始比较,如果谁的ASCII码值大,那么谁大,如果相等继续比较后面的字符,当两个字符不相等时停止比较,此时谁的ASCII码值大谁就大
        返回值
            1
                前面字符串大于后面的字符串
            -1
                前面的字符串小于后面的字符串
            0
                前面的字符串等于后面的字符串

<script type="text/javascript">			
	var str1 = "abcd";
	var str2 = "abdd";
	var retVal = str1.localeCompare(str2);
	if( retVal > 0 ){
		console.log( str1 + ">" + str2 );
	}else if( retVal < 0 ){
		console.log( str1 + "<" + str2 );	//abcd<abdd
	}else{
		console.log( str1 + "=" + str2 );
	}
</script>

indexOf(searchValue, fromindex)
        功能
            从左至右查找第一次出现的字符串
        参数
            searchValue:要查找的字符串
            fromindex:可选参数,表示从指定下标处开始往后查找
        返回值
            子串第一次出现的下标,如果没有子字符串,则返回-1
 lastIndexOf(searchValue, fromindex)
        功能
            从右至左查找第一次出现的字符串
        参数
            searchValue:要查找的字符串
            fromindex:可选参数,表示从指定下标处开始往后查找
        返回值
            子串第一次出现的下标,如果没有子字符串,则返回-1

<script type="text/javascript">			
	var str = "Tom is a handsome boy!Lucy is a beautiful girl!";
	console.log( str.indexOf("is") );			//4
	console.log( str.indexOf("is",6) );			//27
	console.log( str.lastIndexOf( "is" ));		//27
	console.log( str.lastIndexOf("is",-2) );	//-1	
</script>

replace(oldStr, newStr)
        字符串替换
            默认只能替换第一次出现的oldStr
            通过正则表达式可以实现全部替换

<script type="text/javascript">			
	var str = "Tom is a handsome boy!Tom is a handsome boy!";
	var newStr = str.replace( "boy","man" );
	console.log( str );			//Tom is a handsome boy!Tom is a handsome boy!
	console.log( newStr );		//Tom is a handsome man!Tom is a handsome boy!
			
	newStr = str.replace( /boy/g,"man" );	
	console.log( str );			//Tom is a handsome boy!Tom is a handsome boy!
	console.log( newStr );		//Tom is a handsome man!Tom is a handsome man!
</script>

提取子串
        substring(start,stop)
            定义
                substring()方法用于提取字符串中介于两个指定下标之间的字符
            参数
                start
                    必须。一个非负的整数,要提取的子串的第一个字符在原字符中的位置
                end
                    可选。一个非负的整数,要提取的子串的最后一个字符在原字符中的位置+1
                    如果省略该参数,那么返回的子串会一直截取到原字符串的末尾
            返回值
                一个新的字符串,该字符串值包含 stringObject 的一个子字符串,其内容是从 start 处到 stop-1 处的所有字符,其长度为 stop 减 start。
            注意
                substring() 方法返回的子串包括 start 处的字符,但不包括 stop 处的字符
                如果参数 start 与 stop 相等,那么该方法返回的就是一个空串(即长度为 0 的字符串)
                如果 start 比 stop 大,那么该方法在提取子串之前会先交换这两个参数

<script type="text/javascript">			
	var str = "Tom is a handsome boy!Tom is a handsome boy!";
	console.log( str.substring(0,3) );		//Tom
	console.log( str.substring(3,0) );		//Tom
	console.log( str.substring(19) );		//oy!Tom is a handsome boy!
	console.log( str.substring(1,1) );		//空
	console.log( str );			//Tom is a handsome boy!Tom is a handsome boy!
</script>

 substr(start,length)
            定义
                substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
            参数
                start
                    必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。
                length
                    可选。子串中的字符数。必须是数值。如果省略了该参数,那么返回从原字符串的开始位置到结尾的字串。
            返回值
                一个新的字符串,包含从 原字符串的 start(包括 start 所指的字符) 处开始的 length 个字符。如果没有指定 length,那么返回的字符串包含从 start 到原字符串的结尾的字符。
        注意:这两个方法均不会改变原字符串

<script type="text/javascript">			
	var str = "Tom is a handsome boy!Tom is a handsome boy!";
	console.log( str.substr(9,4) );		//hand
	console.log( str.substr(-9,4) );	//some
	console.log( str.substr(19) );		//oy!Tom is a handsome boy!
</script>

stringObject.split(separator,howmany)
        定义
            split() 方法用于把一个字符串分割成字符串数组
        参数
            separator
                    必需。字符串或正则表达式,从该参数指定的地方分割 stringObject。
            howmany
                    可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
        返回值
            一个字符串数组
        注意:如果把空字符串 ("") 用作 separator,那么 stringObject 中的每个字符之间都会被分割

<script type="text/javascript">			
	var str = "Tom is a handsome boy!Tom is a cool boy!";
	console.log( str.split(" ") );		//["Tom", "is", "a", "handsome", "boy!Tom", "is", "a", "cool", "boy!"]
	console.log( str.split(" ",5) );	//["Tom", "is", "a", "handsome", "boy!Tom"]
	console.log( str.split("") );		
	/*["T", "o", "m", " ", "i", "s", " ", "a", " ", "h", "a", "n", "d", "s", "o", "m", "e", " ", "b", "o","y", "!", "T", "o", "m", " ", "i", "s", " ", "a", " ", "c", "o", "o", "l", " ", "b", "o", "y", "!"]*/
	console.log( str );		//Tom is a handsome boy!Tom is a cool boy!
</script>

concat()
        用于将一个或多个字符串拼接起来

<script type="text/javascript">			
	var str1 = "Hello ";
	var str2 = "World!";
	console.log( str1.concat(str2) );	//Hello World!
</script>

trim()
        这个方法会创建一个字符串的副本,删除前缀及后缀的所有空格,然后返回结果
 

<script type="text/javascript">			
	var str1 = "  Hello   ";
	console.log( str1.trim() );		//Hello
	console.log( str1 );			//"  Hello   "
</script>


    
    
    
    参考链接:https://github.com/lidaguang1989/javascript-knowhow
    
    
    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值