zoukankan      html  css  js  c++  java
  • 基本包装类——String类型

      1 //基本包装类型    String 类型
      2 // String类型包含了三个属性和大量的可用内置方法。
      3 
      4 // 属性            描述
      5 // length            返回字符串的字符长度
      6 // constructor        返回创建String对象的函数
      7 // prototype        通过添加属性和方法扩展字符串定义
      8 
      9 var box="fuChong"
     10 alert(box.length);        //属性
     11 alert(box.constructor);
     12 
     13 
     14 
     15 //字符方法
     16 // 方法            描述
     17 // charAt(n)        返回指定索引位置的字符
     18 // charCodeAt(n)    以Unicode编码形式返回指定索引位置的字符
     19 
     20 var box="fuChong";
     21 alert(box.charAt(1));    //索引是从0开始的
     22 alert(box.charCodeAt(3));    //返回的是acssii码
     23 alert(box[2]);    //通过数组方式截取,在IE浏览器会显示undefined,谨慎使用
     24 
     25 
     26 
     27 // 字符串操作方法
     28 // 方法                    描述
     29 // concat(str1...str2)        将字符串参数串联到调用该方法的字符串
     30 // slice(n,m)                返回字符串n到m之间位置的字符串
     31 // substring(n,m)            同上
     32 // substr(n,m)                返回字符串n开始的m个字符串
     33 
     34 var box="fuChong";
     35 alert(box.concat(" is"," a"," man"," !"));    //参数字符串连接
     36 alert(box.slice(3,5));    //ho
     37 alert(box.substring(3,5));    //ho
     38 alert(box.substr(3,5));    //hong    后面没有5个则后面全选
     39 
     40 alert(box.slice(3));    //hong
     41 alert(box.substring(3));    //hong
     42 alert(box.substr(3));    //hong
     43 
     44 alert(box.slice(-3));    //总字符串长度6-3=3,从第3位后开始向后取    ong
     45 alert(box.substr(-3));    //同上
     46 alert(box.substring(-3));    //返回全部字符串    fuChong
     47 
     48 alert(box.slice(2,-1));    //6-1=5    (2,5)    Chon
     49 alert(box.slice(-2,-1));    //6-2=4    6-1=5 (4,5)    o
     50 alert(box.substring(2,-1));    //参数如果是负数,直接为0    (2,0);如果第二个参数比第一个小,则第二个参数提前    (0,2)    fu
     51 alert(box.substr(2,-1));    //第二参数为负,直接0,(2,0)    空
     52 
     53 // PS:IE的JavaScript实现在处理向substr()方法传递负值的情况下存在问题,它会返回原始字符串,使用时要切记
     54 
     55 
     56 
     57 // 字符串位置方法
     58 // 方法                    描述
     59 // indexOf(str,n)        从n开始搜索的第一个str,并讲搜索的索引值返回
     60 // lastIndexOf(str,n)    从n开始搜索的最后一个str,并将搜索的索引值返回
     61 
     62 var box="fuChong is a man";
     63 alert(box.indexOf("a"));    //返回从初始位置搜索a第一次出现的位置,11
     64 alert(box.lastIndexOf("a"));    //返回从末尾位置搜索a第一次出现的位置,14
     65 alert(box.indexOf("a",5));    //从第5个位置开始搜索a第一次出现的位置    11
     66 alert(box.lastIndexOf("a",5));    //从第5个位置开始向前搜索a第一次出现的位置,-1
     67 alert(box.indexOf(","))    //找不到,返回-1
     68 
     69 // 示例:找出全部的a
     70 //存在返回a所在位置索引值,不存在则返回空
     71 var box="fuchong is a man";
     72 var boxarr=[];
     73 var pos=box.indexOf("a");
     74 while(pos>-1){
     75     boxarr.push(pos);
     76     pos=box.indexOf("a",pos+1);
     77 }
     78 alert(boxarr);    //11、14
     79 
     80 
     81 
     82 // 大小写转换方法
     83 // 方法                        描述
     84 // toLowerCase(str)            将字符串全部转换为小写
     85 // toUpperCase(str)            将字符串全部转换为大写
     86 // toLocaleLowerCase(str)        将字符串全部转换为小写,并且本地化
     87 // toLOcaleupperCase(str)        将字符串全部转换为大写,并且本地化
     88 
     89 var box="fuChong is fuChong";
     90 alert(box.toLowerCase());    //全部小写
     91 alert(box.toUpperCase());    //全部大写
     92 alert(box.toLocaleLowerCase());
     93 alert(box.toLOcaleupperCase());
     94 //PS:只有几种语言(如土耳其语)居右地方特有的大小写本地性,一般来说,是否本地化效果都是一致的。
     95 
     96 
     97 
     98 // 字符串的模拟匹配方法
     99 // 方法                            描述
    100 // match(pattern)                    返回pattern中的子串或null
    101 // replace(pattern,replacement)    用replacement替换pattern
    102 // search(pattern)                    返回字符串中pattern开始位置
    103 // split(pattern)                    返回字符串按指定pattern拆分的数组
    104 
    105 var box="fuChong";
    106 alert(box.match("f"));    //找到f即返回f
    107 alert(box.match("z"));    //没有找到z则返回null
    108 alert(box.search("f"));    //找到f的位置
    109 alert(box.replace("f","F"));    //替换  返回FuChong
    110 alert(box.split("u"));    //分割成数组 返回f,Chong
    111 
    112 
    113 
    114 // 其他方法
    115 // 方法                        描述
    116 // fromCharCode(ascii)            静态方法,输入Ascii码对应值
    117 // localeCompare(str1,str2)    比较两个字符串,并返回对应的值
    118 
    119 alert(String.fromCharCode(76));    //L,输出Ascii码对应值
    120 
    121 
    122 //localeCompare(str1,str2)方法详解:比较两个只付出并返回以下值中的一个
    123 //1、如果字符串在字母表中应该排在字符串参数之前,则返回一个负数。(多数-1)
    124 //2、如果字符串等于字符串参数,则返回0。
    125 //3、如果字符串在字母表中应该排在字符串参数之后,则返回一个正数。(多数1)
    126 
    127 var box="fuChong";
    128 alert(box.localeCompare("fuChong"));    //返回0
    129 alert(box.localeCompare("a"));    //返回1
    130 alert(box.localeCompare("z"));    //返回-1
    131 alert(box.localeCompare("付"));    //返回-1,这个弄着玩的
    132 alert(box.localeCompare("1"));    //返回1
    133 
    134 
    135 
    136 // HTML方法
    137 // 方法                描述
    138 // anchor(name)        <a name="name">str</a>
    139 // big()                <big>str</big>
    140 // blink()                <blink>str</blink>
    141 // bold()                <b>str</b>
    142 // fixed()                <tt>str</tt>
    143 // fontcolor(color)    <font color="color">str</font>
    144 // fontsize(size)        <font size="size">str</font>
    145 // link(URL)            <a href="URL">str</a>
    146 // small()                <small>str</small>
    147 // strike()            <strike>str</strike>
    148 // italics()            <i>str</str>
    149 // sub()                <sub>str</sub>
    150 // sup()                <sup>str</sup>
    151 //以上是通过JS生成一个html标签,没什么太大作用。只需了解即可。
    152 
    153 var box="百度";
    154 alert(box.link("http://www.baidu.com"));    //返回<a href="http://www.baidu.com">百度</a>
    View Code
    高否?富否?帅否? 否? 滚去学习!
  • 相关阅读:
    hive默认配置 .hiverc
    hive 行列转换
    hive 全表全字段对比
    shell 获取hive表结构
    粘包现象与解决方案
    win 关闭正在使用的端口
    pycharm格式报错: Remove redundant parentheses
    博客系统作业
    django中间件
    django的用户认证组件
  • 原文地址:https://www.cnblogs.com/baixc/p/3386948.html
Copyright © 2011-2022 走看看