zoukankan      html  css  js  c++  java
  • JavaScript学习——String类型

    String 类型是字符串的对象包装类型

    var stringObjiect = new String("hello world");

    String 类型的每个实例都有一个length属性,表示字符串中包含多个字符

    var stringValue="hello world";
    alert(stringValue.length);//"11"

    注意,即使字符串中包含双字节,每个字符也仍然算一个字符。

    1.字符方法

    两个用于访问字符串中特定字符的方法是charAt()和charCodeAt()

    var stringValue="hello world";
    alert(stringValue.charAt(1));//"e"
    
    var stringValue="hello world";
    alert(stringValue.charCodeAt(1));//输出"101"
    
    var stringValue="hello world";
    alert(stringValue[1]);//"e"

    charCodeAt输出是字符编码。

    方括号方法访问得到除IE7及更早版本以外的所有浏览器的支持。

    2.字符串操作方法

    var stringValue="hello ";
    var result=stringValue.concat("world","!");
    alert(result);//"hello world"
    alert(stringValue);//"hello"
    var stringValue="hello world";
    alert(stringValue.slice(3));//"lo world"
    alert(stringValue.substring(3));//"lo world"
    alert(stringValue.substr(3));//"lo world"
    alert(stringValue.slice(3,7));//"lo w"
    alert(stringValue.substring(3,7));//"lo w"
    alert(stringValue.substr(3,7));//"lo worl"

    3.字符串位置方法

    var stringValue="hello world";
    alert(stringValue.indexOf("o");//4 从头找
    alert(stringValue.lastIndexOf("o");//7 从尾找
    alert(stringValue.indexOf("o",6);//7 从6找
    alert(stringValue.lastIndexOf("o",6);//4 从6往前找

    4.trim()方法

    消除前后的空格

    5.字符串大小写的转换方法

    var stringValue="hello world";
    alert(stringValue.toLocaleUpperCase());//"HELLO WORLD"
    alert(stringValue.toUpperCase());//"HELLO WORLD"
    alert(stringValue.toLocaleLowerCase());//"hello world"
    alert(stringValue.toLowerCase());//"hello world"

    6.字符串的模式匹配方法

    match()

    var pattern=/.at/;
    //与pattern.exec(text)相同
    var matches=text.match(pattern);
    alert(matches.index);//0
    alert(matches[0]);//"cat"
    alert(pattern.lastIndex);//0

     seacrch()

    返回字符串中第一个匹配项的索引;如果没有找到,则返回-1。

    var text="cat,bat,sat,fat";
    var pos=text.search(/at/);
    alert(pos);//1

    replace()

    var text="cat,bat,sat,fat";
    var result=text.replace("at","ond");
    alert(result);//"cond,bat,sat,fat";
    
    result=text.replace(/at/g,"ond");//g为全局标志
    alert(result);//"cond,bond,sond,fond

    split()

    var colorText="red,blue,green,yellow";
    var color1=colorText.split(",");//["red","blue","green","yellow"]
    var color2=colorText.split(",",2);//["red","blue"]

    7.localeCompare()

    比较两个字符串

    var stringValue="yellow";
    alert(stringValue.localeCompare("brick"));//1  brick在yellow前
    alert(stringValue.localeCompare("yellow"));//0 与yellow相等
    alert(stringValue.localeCompare("zoo"));//-1 在yellow后

    8.fromCharCode()

    alert(String.fromCharCode(104,101,108,108,111));//"hello"
  • 相关阅读:
    Tips on Hair and Final gathering
    数学公式和符号的念法
    How to use Intel C++ Compiler in Visual Studio 2008
    Number Prefixes in Mathematics
    Glossy reflections/refractions in large scene
    atomic flushing data
    elvish Template Library Plan
    [Maxim07]中光线与三角形求交算法的推导
    C# 关闭窗体立即停止进程
    MS SQL 索引设计的准则
  • 原文地址:https://www.cnblogs.com/pilee/p/3446394.html
Copyright © 2011-2022 走看看