前端开发_JavaScript_常用对象之String对象

String对象

1.概述

String对象就是我们现实中说的字符串对象,字符串对象是我们开发中使用比较多且比较频繁的一个对象。

String是动态对象,需要创建对象的实例之后才能够使用它的属性或方法。

String 全局对象是一个用于字符串或一个字符序列的构造函数。

2.String对象常用属性

(1).length属性

length 属性表示一个字符串的长度。

(2).length属性的基本使用

<script type="text/javascript">
   var str = "HelloWorld";
   console.log("----字符串长度是:" + str.length);

</script>

 

3.String对象常用方法

  (1).charAt()方法

       charAt() 方法从一个字符串中返回指定的字符。

       基本语法:

str.charAt(index)

       基本使用

var str = "HelloWorld";
		
//使用charAt()获取指定位置的字符串
console.log("the index of 0 position is:" + str.charAt(0) );
console.log("the index of 1 position is:" + str.charAt(1) );
console.log("the index of 2 position is:" + str.charAt(2) );
console.log("the index of 3 position is:" + str.charAt(3) );
console.log("the index of 4 position is:" + str.charAt(4) );
console.log("the index of 5 position is:" + str.charAt(5) );
console.log("the index of 6 position is:" + str.charAt(6) );
console.log("the index of 7 position is:" + str.charAt(7) );
console.log("the index of 8 position is:" + str.charAt(8) );
console.log("the index of 9 position is:" + str.charAt(9) );
console.log("the index of 9 position is:" + str.charAt(10));
console.log("---------------------------------------------");
		 
//使用循环输出字符串内容
for(var i = 0;i < str.length ; i++){
	   console.log("the index of "+ i + " position is:" + str.charAt(i) );
}

 

(2).indexOf()方法

    indexOf() 方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。

    基本语法:

string.idneOf('char');

   基本使用

<script type="text/javascript">
  var str = "HelloWorld";
		
  //使用indexOf()获取指定字符在字符串中的位置
  console.log("the char e position is:" + str.indexOf('e'));
		 
  //如果在一个字符串中出现多个相同的,那么它找的是最先出现的位置
  console.log("the char l position is:" + str.indexOf('l'));
		 
  //如果找的字符没有找到,那么将返回值-1
  console.log("the char t position is:" + str.indexOf('t'));
		 
</script>

(3).substring()方法

    substring() 方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集。

    基本语法:

str.substring(indexStart[, indexEnd])

indexStart
          需要截取的第一个字符的索引,该索引位置的字符作为返回的字符串的首字母。
indexEnd
          可选。一个 0 到字符串长度之间的整数,以该数字为索引的字符不包含在截取的字符串内。

   基本使用

<script type="text/javascript">
  var str = "HelloWorld";
		
  //使用indexOf()获取指定字符在字符串中的位置
  console.log("the subString is:" + str.substring(3));    //loWorld
  console.log("the subString is:" + str.substring(0,3));  //Hel
  console.log("the subString is:" + str.substring(2,5));  //llo
  console.log("the subString is:" + str.substring(0,str.length)); //HelloWorld
  console.log("the subString is:" + str.substring(0,str.length -2 )); //HelloWor
</script>

(4).toLowerCase()/toUpperCase()方法

    toLowerCase() 会将调用该方法的字符串值转为小写形式,并返回。

 toUpperCase() 方法将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)。

    基本语法:

str.toUpperCase();

str.toLowerCase();

  基本使用

<script type="text/javascript">
   var str = "HelloWorld你好世界";
		
   //进行大小写转化
   console.log(str.toLowerCase());   //helloworld你好世界
   console.log(str.toUpperCase());      //HELLOWORLD你好世界
</script>

(5).includes()方法

   includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。

    基本语法:

str.includes(searchString[, position])

searchString
            要在此字符串中搜索的字符串。
position 可选
           从当前字符串的哪个索引位置开始搜寻子字符串,默认值为 0。

  基本使用

<script type="text/javascript">
   var str = "HelloWorld你好世界";
		
   //字符串的包含情况说明
   console.log("-----" + str.includes('He'));   //true
   console.log("-----" + str.includes('he'));   //false
   console.log("-----" + str.includes('he',1)); //false
	
</script>

(6).splite()方法

  split() 方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置。 

      基本语法:

str.split([separator[, limit]])

separator
指定表示每个拆分应发生的点的字符串。separator 可以是一个字符串或正则表达式。 如果纯文本分隔符包含多个字符,则必须找到整个字符串来表示分割点。如果在str中省略或不出现分隔符,则返回的数组包含一个由整个字符串组成的元素。如果分隔符为空字符串,则将str原字符串中每个字符的数组形式返回。

limit
一个整数,限定返回的分割片段数量。当提供此参数时,split 方法会在指定分隔符的每次出现时分割该字符串,但在限制条目已放入数组时停止。如果在达到指定限制之前达到字符串的末尾,它可能仍然包含少于限制的条目。新数组中不返回剩下的文本。

返回值
返回源字符串以分隔符出现位置分隔而成的一个 Array 

<script type="text/javascript">
   var str = "HelloWorld你好世界";
   //字符串的完全拆分
   var result = str.split("l");
   console.log("完全拆解结果是:" + result);  //He,,oWor,d你好世界
   //字符串的部分拆分
   var result = str.split("l",2);   //He,
   console.log("部分拆分:" + result);
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔笛手7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值