zoukankan      html  css  js  c++  java
  • 字符串操作的相关方法

    var string='hello world';
    console.log(string.charAt(3));//下标从0开始
    var string1=string.concat('lww');
    console.log(string1);
    var string2=string.concat('lww','kk','!');
    console.log(string2);
    var string4=string.substr(3);
    console.log(string4);//lo world
    var string5=string.substring(3);
    console.log(string5);//lo world
    var string6=string.slice(3);
    console.log(string6)//lo world
    var string7=string1.slice(3,7);
    console.log(string7);//lo w
    var string8=string.substr(3,7);
    console.log(string8);//lo worl
    var string9=string.substring(3,7);
    console.log(string9);//lo w
    var string10=string.indexOf('l');
    console.log(string10);//2
    var string11=string.lastIndexOf('l');
    console.log(string11);//9
    var string12=string.indexOf('o',6);
    console.log(string12);//7从第6个位置开始搜寻,忽略前面的字符
    var string13='hello world this is a string html you can see it';
    var position=new Array();
    var pos=string13.indexOf('i');
    while(pos>-1){
    position.push(pos);
    pos=string13.indexOf('i',pos+1);
    }
    console.log(position);//所有i的位置组成的数组

    //trim()
    var string14=' hello world ';
    console.log(string14);
    var string15=string14.trim();
    console.log(string15);//hello world

    //转换大小写方法
    var string16=string.toLocaleUpperCase();
    console.log(string16);//HELLO WORLD
    var string17=string.toUpperCase();
    console.log(string17);//HELLO WORLD
    var string18=string17.toLowerCase();
    console.log(string18);//hello world
    var string19=string17.toLocaleLowerCase();
    console.log(string19);//hello world

    //replace()替换方法
    var string20=string.replace('l','k');
    console.log(string20);//heklo world将第一个匹配到的l替换成K

    //search()方法跟indexOf()有点相似
    var string21=string.search('l');
    console.log(string21);//返回第一个l的位置 2

    //split 方法根据指定的分隔符来将字符串分成多个字符串并组成数组
    var string22=string.split(' ');
    console.log(string22);//根据指定的空格的字符串来把字符串分成多个字符串并组成数组['hello','world']
    var string23='red,yellow,green,blue';
    var string24=string23.split(',');
    console.log(string24);//['red','yellow','green','blue'];
    var string25=string23.split(',',1);
    console.log(string25);//['red'];这个数字1就代表保留一个字符串,要是3的话就保留3个字符串['red,'yellow','blue'];

    //localeCompare()
    var string26='red';
    var string27=string26.localeCompare('yellow');
    console.log(string27)//-1因为在字母表中y在r的后面 所有未负数-1
    var string28=string26.localeCompare('red');
    console.log(string28);//0因为字母相同
    var string29=string26.localeCompare('ahh');
    console.log(string29)//1因为字母a在前
    var string30=string26.localeCompare('rff');
    console.log(string30);//-1第一个字母相同则比较第二个

  • 相关阅读:
    常见二叉树问题
    leetcode上回溯法的使用
    搜索+回溯
    navicat连接登录windows10本地wsl的数据库
    皇后问题
    拓扑排序
    三路排序算法
    在 ServiceModel 客户端配置部分中,找不到引用协定“WebServiceTest.WebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素
    WCF异常信息
    C# 制作ZIP压缩包
  • 原文地址:https://www.cnblogs.com/lwwen/p/5586548.html
Copyright © 2011-2022 走看看