• javascript中的字符串对象


    1.javascript的对象的概念

    javascript中,除了nullundefined以处,其他的数据类型都被定义成了对象

    也可以用创建对象的方法定义变量,string,math,array,data都是javascript中重要的内置对象.

    javascript程序大多数功能都是基于对象实现的

    var aa=Number.MAX_VALUE;                                //利用数字对象获取可表示最大数
    var bb=new String("hello world");                       //创建字符串对象
    var cc=new Date();                                      //创建日期对象
    var dd=new Array("monday","tuesday","thirsday");        //数组对象
    

    2.javascript内置对象的分类

    2.1数据对象

    Number          数字对象
    String          字符串对象
    Boolean         布尔值对象
    

    2.2组合对象

    Array           数组对象
    Math			数学对象
    Date			日期对象
    

    2.3高级对象

    Object          自定义对象
    Error           错误对象
    Function		函数对象
    Regexp          正则表达式对象
    Global          全局对象
    

    3.javascript中的字符串(String)对象

    3.1 字符串对象的创建

    字符串的创建有两种方式:

    1.变量="字符串"
    2.字符串对象名称=new String(字符串)
    

    例子:

    var str1="hello world";
    var str1= new String("hello word");
    

    3.2 字符串对象的属性和函数

    str.length

    获取字符串的长度

    例子:

    var txt="Hello World!";
    document.write(txt.length);
    

    返回:

    12
    

    str.toLowerCase()

    把字符串转换为小写

    例子:

    var str="Hello World!";
    document.write(str.toLowerCase());
    

    返回:

    hello world!
    

    str.toUpperCase()

    把字符串转换为大写

    例子:

    var str="Hello World!";
    document.write(str.toUpperCase());
    

    返回:

    HELLO WORLD!
    

    str.trim()

    去除字符串两边的空格

    例子:

    var str1="    hello world   ";
    document.write(str1.length + "<br />");
    document.write(str1.trim() + "<br />");
    document.write(str1.trim().length);
    

    返回值:

    18
    hello world
    11
    

    str.charAt(index)

    返回指定索引的字符,字符串的第一个字符串的下标为0

    例子:

    var str="Hello world!";
    document.write(str.charAt(1));
    

    返回:

    e
    

    str.indexOf(findstr,index)

    返回指定字符在字符串中首次出现的位置

    从字符串strindex处开始,查找findstr,如果找到则返回findstr第一次出现的位置,如果没有指定index,则从头开始查找,如果没有找到要查找的字符串,返回-1,大小写敏感

    例子:

    var str="Hello world!";
    document.write(str.indexOf("Hello") + "<br />");
    document.write(str.indexOf("World") + "<br />");
    document.write(str.indexOf("world") + "<br />");
    

    返回值:
    0
    -1
    6

    str.lastIndexOf(findstr,index)

    在字符串str中的index处向前查找指定字符findstr,没有指定index时,则从后向前查找,如果找到findstr,则返回第一个findstr在字符串str的位置.

    如果没找到指定指定字符串,则返回-1,大小写敏感

    例子:

    var str="Hello world!";
    document.write(str.lastIndexOf("Hello") + "<br />");
    document.write(str.lastIndexOf("World") + "<br />");
    document.write(str.lastIndexOf("world"));
    

    返回:

    0
    -1
    6
    

    str.match(findstr)

    在字符串中查找指定的字符,这个字符可以是正则表达式

    若在字符串str中找到指定的字符串,则返回找到的字符串,没找到则返回null

    例子:

    var str="Hello world!";
    document.write(str.match("world") + "<br />");
    document.write(str.match("World") + "<br />");
    document.write(str.match("worlld") + "<br />");
    document.write(str.match("world!"));
    

    返回:

    world
    null
    null
    world!
    

    str.search(regexp)

    在字符串str中查找指定的子字符串或与正则表达式匹配的子字符串

    返回指定子字符串在字符串str的起始位置,未匹配到子字符串则返回-1,大小写敏感

    例子:

    var str="hello world!";
    document.write(str.search(/world/));
    document.write(str.search(/World/));
    

    返回:

    6
    -1
    

    str.substr(start,length)

    从字符串str的起始索引处开始提取指定长度的字符串

    如果没有指定长度,则提取从start处开始到结尾的全部字符

    例子1:

    var str="Hello world!";
    document.write(str.substr(3));
    

    返回:

    lo world!
    

    例子2:

    var str="Hello world!";
    document.write(str.substr(3,7));
    

    返回:

    lo worl
    

    str.substring(start,end)

    提取字符串str中两个索引之间的字符串,不包括end处的字符
    如果startend的值相等,则返回一个空的字符串

    例子1:

    var str="Hello world!";
    document.write(str.substring(3));
    

    返回:

    lo world!
    

    例子2:

    var str="Hello world!";
    document.write(str.substring(3,7));
    

    返回:

    lo w
    

    str.slice(start,end)

    对字符串进行切片操作,返回字符串strstart(包含start)开始,到end(不包括end)结尾的所有字符

    例子1:

    var str="Hello happy world!";
    document.write(str.slice(6));
    

    返回:

    happy world!
    

    例子2:

    var str="Hello happy world!";
    document.write(str.slice(6,11));
    

    返回:

    happy
    

    str.replace(oldstr,newstr)

    把字符串中str的oldstr替换成newstr

    例子:

    var str="hello world!";
    document.write(str.replace(/world/, "javascript"));
    

    返回:

    hello javascript!
    

    str.split(sep,num)

    把字符串strsep分割成字符串数组,num为可分割的最大数

    例子1:

    var str="How are you doing today?";
    document.write(str.split(" ") + "<br />");
    document.write(str.split("") + "<br />");
    document.write(str.split(" ",3));
    

    返回:

    How,are,you,doing,today?
    H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
    How,are,you
    

    例子2:

    "2:3:4:5".split(":");	//将返回["2", "3", "4", "5"]
    "|a|b|c".split("|");	//将返回["", "a", "b", "c"]
    "hello".split("");	//可返回 ["h", "e", "l", "l", "o"]
    "hello".split("", 3);	//可返回 ["h", "e", "l"]
    

    str.concat(str1,str2...)

    连接两个或多个字符串

    例子:

    var str1="Hello ";
    var str2="world!";
    document.write(str1.concat(str2));
    

    返回:

    Hello world!
  • 相关阅读:
    伪元素和伪类的区别
    绝对定位是相对定位元素的什么边界进行定位的?
    元素如何设置滚动/滚动条的设置与隐藏
    什么是行内元素、块级元素?
    通过JS判断当前浏览器的类型
    获取元素计算样式getComputedStyle()与currentStyle
    javascript链式运动框架案例
    任意值的运动框架
    JS多物体运动案例:变宽、变高
    offsetWidth与offsetHeight
  • 原文地址:https://www.cnblogs.com/renpingsheng/p/7308697.html
Copyright © 2020-2023  润新知