web前端javaScript笔记——(9)包装类和字符串

发布时间:2024年01月09日
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>
       
    </title>
    <style>
    
    </style>
    <script type="text/javascript">
     /*基本数据类型
      String Number Boolean Null Undefined
               引用数据类型
       Object
       在JS中为我们提供了三个包装类u,通过这三个包装类可以将基本数据类型转换为对象
      String() 可以将基本数据类型字符串转换为String对象
      Number() 可以将基本数据类型的数字转换为Number对象
      Boolean()可以将基本数据类型的boolean值转换为Boolean对象
      String() Number() Boolean()首字母大写,都是构造函数
            但是注意,我们在实际应用中不会使用基本数据类型的对象
            如果使用基本数据类型的队形,在做一些比较的时候可能会带来一些不可预期的结果
      */
     var a=123;
     console.log(typeof a); //number
     //创建一个Number类型的对象
     var num=new Number(3);
     console.log(typeof num);   //object
     var str=new String("hello")  //object
     var bool=new Boolean(true);    //object
     
     //向num中添加一个属性
     //因为number是对象,所以可以给对象中添加属性
     num.hello="abcdefg";
     console.log(num.hello);  //abcdefg
     
     //基本数据类型不可添加属性
     var a=3;
     a.hello="你好";
     console.log(a.hello);   //undefined
     
     //两个对象比较时,比较的是对象的地址,所以结果为false
     var num=new Number(3);
     var num1=new Number(3);
     console.log(num==num1);    //false
     
     var bool2=true;
     console.log(bool==bool2);  //true  比较时自动做了类型转换
     console.log(bool===bool2);  //false
     
     /**不管()里面是true还是false,都会运行,因为b是对象,它转换为布尔值永远是true/
     var b=new Boolean(false);
     if(b){
         alert("我运行了");
     }
     /*方法和属性只能添加给对象,不能添加给基本数据类型
      当我们对一些基本数据类型的值去调用属性和方法的时候,
      浏览器会临时使用包装类将其转换为对象,然后再调用对象的属性和方法
      调用完之后,再将器转换为基本数据类型*/    
     var s=123;
     s=s.toString();  //这里是一个临时的转换
     console.log(s);  //123
     console.log(typeof s); //string
     
     //临时的类型转换,可以添加进去,读取不出来,第一次转换,添加进去之后就销毁了
     s.hello="你好";
     console.log(s.hello); //第二次转换,不是一回事  //undefined这两次转换的是不同对象
     
    </script>
</head>
<body>
   
    
</body>
</html>

String对象方法

anchor() 创建 HTML锚

big() 用大号字体显示字符串。

blink() 显示闪动字符串。

bold() 使用粗体显示字符串。

charAt() 返回在指定位置的字符。

charCodeAt() 返回在指定的位置的字符的 Unicode 编码。

concat() 连接字符串。

fixed() 以打字机文本显示字符串。

fontcolor() 使用指定的颜色来显示字符串。

fontsize() 使用指定的尺寸来显示字符串。

fromCharCode() 从字符编码创建一个字符串。

indexof() 检索字符串。

italics() 使用斜体显示字符串。

lastIndexOf() 从后向前搜索字符串。

link() 将字符串显示为链接

localeCompare() 用本地特定的顺序来比较两个字符串

match() 找到一个或多个正则表达式的匹配

replace() 替换与正则表达式相匹配的值

search() 检索与正则表达式相匹配的值

slice() 提取字符串的片段,并在新的字符串中返回被提取的部分

small() 使用小字号来显示字符串

split() 把字符串分割为字符串数组

strike() 使用删除线来显示字符串

sub() 把字符串显示为下标

substr() 从起始索引号提取字符除按中指定数目的字符

substring() 提取字符串中两个指定的索引号之间的字符

sup() 把字符串显示为上标

toLocaleLowerCase() 把字符串转换为小写

toLocaleUpperCase() 把字符串转换为大写

toLowerCase() 把字符串转换为小写

toUpperCase() 把字符串转换为大写

toSource() 代表对象的源代码

toString() 返回字符串

valueOf() 返回某个字符串对象的原始值

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>
       
    </title>
    <style>
    
    </style>
    <script type="text/javascript">
    	//数组的大部分方法不会对原数组产生影响
    //创建一个字符串
    var str="hello Atguigu";
    /*再底层字符串是以字符数组的像是保存的*/
    //["h","e","l".......]
    /*字符串的方法和数组的方法类似,基本数据类型可以
     * 调用string(),因为它会做一个临时的类型转换*/
    console.log(str.length);  //13
    console.log(str[0]);   //h
    //charAt()可以返回字符串中指定位置的字符,根据缩影获取指定的字符
    var result=str.charAt(0);
    console.log(result);       //h
    //charCodeAt()获取指定位置字符的字符编码(unicode编码)
    result=str.charCodeAt(0);  //104
    console.log(result);
    /*String.formCharCode()
            可以根据字符编码去获取字符
     fromCharCode属于构造对象,需要根据构造对象去调用*/
     result=String.fromCharCode(20013);  //中
     console.log(result);
      result=String.fromCharCode(0x16);  //十六进制
     console.log(result);
     /*concat()
             可以用来连接两个或多个字符串
             作用和+一样*/
      result=str.concat("你好","再见");
      console.log(result);  //hello Atguigu你好再见
      /*indexOf()
                该方法可以检索一个字符串中是否含有指定内容
                如果字符串中含有该内容, 则返回第一次出现的索引
                如果没有找到指定的内容,则返回-1
                可以指定一个第二个参数,指定开始查找的位置
                
       lastindexOf()
                  该方法的用法和indexOf一样
                   不同的是indexOf是从前往后找
       	 而lastIndexOf是从后往前找
       	 也可以指定开始查找的位置
       	 */
       str="hello atiguigu";
       result=str.indexOf("h");
       console.log(result);   //0   0为h的索引
       result=str.indexOf("p");
       console.log(result);   //-1 没有找到
       /*slice()
                 可以从字符串中截取指定的内容
                 不会影响原字符串,而是将截取到的内容返回
                 参数:
                 第一个,开始位置的索引(包括开始位置)
                 第二个,结束位置的索引(不包括结束位置)
         如果省略了第二个参数,则会截取到后边所有的
         也可以传递一个负数作为参数,负数的话将会从后边计算
                 **/
       str="abcdefgsfggh"
       result=str.slice(1,4);
       console.log(result);     //bcd
       result=str.slice(1,-1);
       console.log(result);     //bcdefgsfgg
       result=str.slice(1,-3);
       console.log(result);     //bcdefgsf
       result=str.slice(1);
       console.log(result);     //bcdefgsfggh     
       /*subString()            推荐使用
                    可以用来截取一个字符串,可以和slice()类似
                    参数:
                    第一个,开始截取位置的索引(包括开始位置)
                    第二个,结束位置的缩影(不包括结束位置)
                    不同的是这个方法不能接受负值作为参数
                    如果传递了一个负值,则默认使用0
                    而且它还自动调整参数的位置,如果第二个参数小于第一个,则自动交换*/
         result=str.substring(1,0);  //(1,0)会自动变成(0,1)
         console.log(result);        //a
         /*substr()
                        用来截取字符串
                        参数:
          1.截取开始位置的索引
          2.截取的长度*/
         str="abdefggg";
         result=str.substr(3,2);
         console.log(result);       //ef
         /*split()
         	 可以将一个字符串拆分为一个数组
         	 参数-需要一个字符串作为参数,将会根据该字符串去拆分数组*/
         	str="abc,bcd,efg,hij";
         	result=str.split();
         	console.log(result);//['abc,bcd,efg,hij']
         	result=str.split(",");
         	console.log(result);//['abc,bcd,efg,hij']  
         	/*[
   			 "abc",
   			 "bcd",
   			 "efg",
   			 "hij"
				]*/
				
		    console.log(Array.isArray(result));    //true
		    console.log(result[0]);                 //abc
		    result=str.split("d");
         	console.log(result.length);     //2
         	console.log(result);
		    /*[
              "abc,bc",
              ",efg,hij"
               ]*/
		     str="dfgggsghh"; 
		     //如果传递一个空串作为参数,则会将每个字符都拆分为数组中的一个元素
		     result=str.split("");
         	console.log(result); 
         	/*[
    "d",
    "f",
    "g",
    "g",
    "g",
    "s",
    "g",
    "h",
    "h"
]*/
         	
      
     

    </script>
   
</head>
<body>
   
    
</body>
</html>

文章来源:https://blog.csdn.net/JLY19970726/article/details/135466340
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。