简介:

在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String、Math、Array、Date、RegExp都是JavaScript中重要的内置对象,在JavaScript程序大多数功能都是基于对象实现的

<script language="javascript">
var aa=Number.MAX_VALUE; 
//利用数字对象获取可表示最大数
var bb=new String("hello JavaScript"); 
//创建字符串对象
var cc=new Date();
//创建日期对象
var dd=new Array("星期一","星期二","星期三","星期四"); 
//数组对象
</script>

 JavaScript(JS)之Javascript对象

一、string对象(字符串)

1.字符串对象创建

字符串创建(两种方式)
       ① 变量 = “字符串”
       ② 字串串对象名称 = new String (字符串)

//        ========================
//        字符串对象的创建有两种方式
//        方式一
          var s = 'sffghgfd';
//        方式二
          var s1 = new String('  hel lo ');
          console.log(s,s1);
          console.log(typeof(s)); //object类型
          console.log(typeof (s1)); //string类型

 

2.字符串对象的属性和函数

-------属性
x.length ----获取字符串的长度
------方法 x.toLowerCase() ----转为小写 x.toUpperCase() ----转为大写 x.trim() ----去除字符串两边空格 ----字符串查询方法 x.charAt(index) ----str1.charAt(index);----获取指定位置字符,其中index为要获取的字符索引 x.indexOf(index)----查询字符串位置 x.lastIndexOf(findstr) x.match(regexp) ----match返回匹配字符串的数组,如果没有匹配则返回null x.search(regexp) ----search返回匹配字符串的首字符位置索引 示例: var str1="welcome to the world of JS!"; var str2=str1.match("world"); var str3=str1.search("world"); alert(str2[0]); // 结果为"world" alert(str3); // 结果为15 ----子字符串处理方法 x.substr(start, length) ----start表示开始位置,length表示截取长度 x.substring(start, end) ----end是结束位置 x.slice(start, end) ----切片操作字符串 示例: var str1="abcdefgh"; var str2=str1.slice(2,4); var str3=str1.slice(4); var str4=str1.slice(2,-1); var str5=str1.slice(-3,-1); alert(str2); //结果为"cd" alert(str3); //结果为"efgh" alert(str4); //结果为"cdefg" alert(str5); //结果为"fg" x.replace(findstr,tostr) ---- 字符串替换 x.split(); ----分割字符串 var str1="一,二,三,四,五,六,日"; var strArray=str1.split(","); alert(strArray[1]);//结果为"二" x.concat(addstr) ---- 拼接字符串

方法的使用

 1     <script>
 2 //        ========================
 3 //        字符串对象的创建有两种方式
 4 //        方式一
 5           var s = 'sffghgfd';
 6 //        方式二
 7           var s1 = new String('  hel lo ');
 8           console.log(s,s1);
 9           console.log(typeof(s)); //object类型
10           console.log(typeof (s1)); //string类型
11 
12 //        ======================
13 //        字符串对象的属性和方法
14 //           1.字符串就这么一个属性
15         console.log(s.length);  //获取字符串的长度
16 
17 //           2.字符串的方法
18         console.log(s.toUpperCase()) ; //变大写
19         console.log(s.toLocaleLowerCase()) ;//变小写
20         console.log(s1.trim());  //去除字符串两边的空格(和python中的strip方法一样,不会去除中间的空格)
21 ////           3.字符串的查询方法
22         console.log(s.charAt(3));  //获取指定索引位置的字符
23         console.log(s.indexOf('f')); //如果有重复的,获取第一个字符的索引,如果没有你要找的字符在字符串中没有就返回-1
24         console.log(s.lastIndexOf('f')); //如果有重复的,获取最后一个字符的索引
25         var str='welcome to the world of JS!';
26         var str1 = str.match('world');  //match返回匹配字符串的数组,如果没有匹配则返回null
27         var str2 = str.search('world');//search返回匹配字符串从首字符位置开始的索引,如果没有返回-1
28         console.log(str1);//打印
29         alert(str1);//弹出
30         console.log(str2);
31         alert(str2);
32 
33 
34 //        =====================
35 //        子字符串处理方法
36         var aaa='welcome to the world of JS!';
37         console.log(aaa.substr(2,4)); //表示从第二个位置开始截取四个
38         console.log(aaa.substring(2,4)); //索引从第二个开始到第四个,注意顾头不顾尾
39         //切片操作(和python中的一样,都是顾头不顾尾的)
40         console.log(aaa.slice(3,6));//从第三个到第六个
41         console.log(aaa.slice(4)); //从第四个开始取后面的
42         console.log(aaa.slice(2,-1)); //从第二个到最后一个
43         console.log(aaa.slice(-3,-1));
44 
45 
46 //        字符串替换、、
47         console.log(aaa.replace('w','c')); //字符串替换,只能换一个
48         //而在python中全部都能替换
49         console.log(aaa.split(' ')); //吧字符串按照空格分割
50         alert(aaa.split(' ')); //吧字符串按照空格分割
51         var strArray = aaa.split(' ');
52         alert(strArray[2])
53     </script>
使用方法

相关文章:

  • 2021-12-25
  • 2021-12-25
  • 2021-06-16
  • 2021-09-20
  • 2022-01-16
  • 2022-12-23
  • 2021-10-26
猜你喜欢
  • 2022-01-22
  • 2021-08-11
  • 2021-09-17
  • 2021-12-05
  • 2022-02-17
  • 2022-03-01
  • 2021-11-15
相关资源
相似解决方案