zoukankan      html  css  js  c++  java
  • javaScript——String类型

    1、字符方法

    charAt()、chartCodeAt():都接收一个参数:字符位置

    var stringValue = 'Hello World';
    
    //charAt()返回给定位置的字符
    console.log(stringValue.charAt(1));//"e"
    
    //[]加字符索引方位字符的方法
    console.log(stringValue[1]);//"e"
    
    //charCodeAt()返回给定位置字符的字符编码
    console.log(stringValue.charCodeAt(1));//101

    2、操作方法

    concat():将一个或多个字符串拼接起来,返回新字符串

    slice()、substring()、substr():都接收1或2个参数:开始位置、结束位置,只有一个参数时,将末尾作为结束位置。返回操作后的新字符串(不包括结束位置的字符)

    substring():当第二个参数小于第一个参数时,始终以小的作为起始位置。

    substr():接收1或2个参数:开始位置、返回字符串的个数。返回操作后的新字符串

    var str = 'Hello World';
    
    /**
    *参数为正数
    */
    //slice()
    console.log(str.slice(3,7));//"lo W"
    
    //substr()
    console.log(str.substr(3,7));//"lo Worl"
    
    //substring()
    console.log(str.substring(3,7));//"lo W"
    
    /**
    *参数为负数
    */
    //slice():将负值与字符串的长度相加
    console.log(str.slice(3,-4));//"lo W"
    
    //substr():将负的第一个参数与字符串长度相加,负的第二个参数转换为0
    console.log(str.substr(3,-4));//""
    
    //substring():把所有的负值都转换为0
    console.log(str.substring(3,-4));//"Hel"

    3、位置方法

    indexOf()、lastIndexOf():接收1或2个参数:给定的字符串、开始查找的位置,返回字符串的位置(如果没有则返回-1)

    var str = 'hello world';
    
    //indexOf():从头往后找
    console.log(str.indexOf("o"));//4
    
    //lastIndexOf():从后往头找
    console.log(str.lastIndexOf("o"));//7
    
    /**
    *通过循环得到字符所有的位置
    *思路:从头开始找到第一个位置后,以这个位置的后一项最为起始位置继续找,直到返回-1
    */
    var position = [];
    var pos = str.indexOf("l");
    while(pos > -1){
          position.push(pos);
          pos = str.indexOf("l",pos+1);  
    }
    console.log(position);//2,3,9

    4、删除空格

    trim():删除字符串前置或后缀的所有空格;

    5、大小写转换

    toLowerCase()、toLocaleLowerCase():转换为小写

    toUpperCase()、toLocaleUpperCase():转换为大写

    toLocaleLowerCase()、toLocaleUpperCase()针对特定地区实现(推荐使用)

    6、模式匹配方法

    match():与调用RegExp的exec()方法相同,只接收一个参数:正则表达式或者RegExp对象。返回一个数组

    search():与match方法接收参数相同,返回字符串中第一个匹配项的索引

    replace():接收两个参数:第一个参数可以是一个RegExp对象或者一个字符串,第二个参数可以是一个字符串或者一个函数。

    split():基于指定的分隔符将字符串分割成多个字符串,并将结果放于一个数组中。接收两个参数:分隔符字符串或者RegExp对象、数组的大小

    var text = 'cat, bat, sat, fat';
    
    //match()
    var reg_1 = /.at/;
    //与reg_1.exec(text)方法相同
    var matches = text.match(reg_1);
    console.log(matches[0]);//cat 
    
    //search
    console.log(text.search(/at/));//1
    
    //replace()
    var result = text.replace('at','ond');
    console.log(result);//'cond, bat, sat, fat'
    result = text.replace(/at/g,'ond');
    console.log(result);//'cond, bond, sond, fond'
    
    //split()
    var textArr = text.split(', ');
    console.log(textArr);["cat","bat","sat","fat"]

    7、

    localeCompare():比较两个字符串,返回下列值中的一个

    如果字符串在字母表中应该排在字符串参数之前,则返回一个负数

    如果字符串等于字符串参数,则返回0

    如果字符串在字母表中应该排在字符串参数之后,则返回一个正数

    var str = 'yellow';
    console.log(str.localeCompare('boy'));//1
    console.log(str.localeCompare('yellow'));//0
    console.log(str.localeCompare('zoom'));//-1

    8、

    fromCharCode():接收一或多个字符编码,将他们转换成字符串

  • 相关阅读:
    toj 1410. Euclid's Game 夜
    hdu 1404 Digital Deletions 夜
    hdu 1536 SNim 夜
    1180. Stone Game 夜
    hdu 1729 Stone Game 夜
    一个有价值的 Sharepoint WebPart 页签部件
    开始 MS Project & P3 E/C 探讨历程!
    cvccomplextype.3.2.2: Attribute 'singleton' is not allowed to appear in element 'bean
    启迪技术
    上传图片:
  • 原文地址:https://www.cnblogs.com/hess/p/9355738.html
Copyright © 2011-2022 走看看