https://www.cnblogs.com/haiyan123/p/7594046.html

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

 

//利用数字对象获取可表示最大数
var a = Number.MAX_VALUE;
console.log(a);
//创建字符串对象
var bb = new String("Hello JS");
console.log(bb);
//创建日期对象
var cc = new Date();
console.log(cc);
//数组对象
var dd = new Array(1,2,3,4);
console.log(dd);

 

1.7976931348623157e+308
String {"Hello JS"}
0: "H"1: "e"2: "l"3: "l"4: "o"5: " "6: "J"7: "S"
length: 8
__proto__:
 String[[PrimitiveValue]]: "Hello JS"
 Sun Mar 17 2019 07:11:00 GMT+0800 (中国标准时间)
 (4) [1, 2, 3, 4]
0: 1
1: 2
2: 3
3: 4
length: 4
__proto__: Array(0)

 

JavaScript(JS)之Javascript对象BOM,History,Location,Function...(二)

一、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)         ----    拼接字符串
<script>
//        ========================
//        字符串对象的创建有两种方式
//        方式一
          var s = 'sffghgfd';
//        方式二
          var s1 = new String('  hel lo ');
          console.log(s,s1);
          console.log(typeof(s)); //object类型
          console.log(typeof (s1)); //string类型

//        ======================
//        字符串对象的属性和方法
//           1.字符串就这么一个属性
        console.log(s.length);  //获取字符串的长度

//           2.字符串的方法
        console.log(s.toUpperCase()) ; //变大写
        console.log(s.toLocaleLowerCase()) ;//变小写
        console.log(s1.trim());  //去除字符串两边的空格(和python中的strip方法一样,不会去除中间的空格)
////           3.字符串的查询方法
        console.log(s.charAt(3));  //获取指定索引位置的字符
        console.log(s.indexOf('f')); //如果有重复的,获取第一个字符的索引,如果没有你要找的字符在字符串中没有就返回-1
        console.log(s.lastIndexOf('f')); //如果有重复的,获取最后一个字符的索引
        var str='welcome to the world of JS!';
        var str1 = str.match('world');  //match返回匹配字符串的数组,如果没有匹配则返回null
        var str2 = str.search('world');//search返回匹配字符串从首字符位置开始的索引,如果没有返回-1
        console.log(str1);//打印
        alert(str1);//弹出
        console.log(str2);
        alert(str2);


//        =====================
//        子字符串处理方法
        var aaa='welcome to the world of JS!';
        console.log(aaa.substr(2,4)); //表示从第二个位置开始截取四个
        console.log(aaa.substring(2,4)); //索引从第二个开始到第四个,注意顾头不顾尾
        //切片操作(和python中的一样,都是顾头不顾尾的)
        console.log(aaa.slice(3,6));//从第三个到第六个
        console.log(aaa.slice(4)); //从第四个开始取后面的
        console.log(aaa.slice(2,-1)); //从第二个到最后一个
        console.log(aaa.slice(-3,-1));


//        字符串替换、、
        console.log(aaa.replace('w','c')); //字符串替换,只能换一个
        //而在python中全部都能替换
        console.log(aaa.split(' ')); //吧字符串按照空格分割
        alert(aaa.split(' ')); //吧字符串按照空格分割
        var strArray = aaa.split(' ');
        alert(strArray[2])
    </script>
使用方法

相关文章: