您的位置:首页 > Web前端 > JavaScript

javascript内置对象-自定义对象与string对象

2015-03-14 00:00 477 查看
摘要: String对象

①String对象用于处理已有的字符串,字符串可以使用单引号或双引号。

②在字符串中查找字符串 indexOf()

var str="hello world"; document.write(str.indexOf("world")) 能查到“world” 返回下标,刚才的代码中返回6.如果没查到返回-1.

自定义对象
第一种方式:
people=new Object();
people.name="java"
people.age=20
document.write("name"+people.name+",age"+people.age);
第二种方式
people={name:java,age:20}
document.write("name"+people.name+",age"+people.age);
第三种方式
function people(name,age){
this.name=name;
this.age=age;
}
son=new people("java","20");
document.write("name"+son.name+",age"+son.age)


String对象
①String对象用于处理已有的字符串,字符串可以使用单引号或双引号。
②在字符串中查找字符串 indexOf()
var str="hello world";
document.write(str.indexOf("world"))  能查到“world” 返回下标,刚才的代码中返回6.如果没查到返回-1.
③匹配字符串 match()
var str="hello world";
document.write(str.match("world"));   能匹配到就直接返回匹配的字符串,刚才的代码返回world,如果没有就返回null。
④更换内容 replace()
var str="hello world";
document.write(str.replace("world","java"));   两个参数,第一个是原本的内容,第二个是替换的内容,刚才的代码返回“hello java”。

⑤大小写转换 toUpperCase()/toLowerCase()
var str="hello world"
document.write(str.toUpperCase());  全部转换成大写,另一个则是全部转换成小写。
⑥字符串转换成数组 split()
var str="hello,java";
var s=str.split(",");
document.write(s[1]);    split的参数是一个逗号,作为一个分割符,刚才的代码返回 “world”
附上更多常用的方法,请参照上文自己试验吧:
属性:length,prototype,constructor
方法:charAt(),charCodeAt(),concat(),fromCharCode(),lastIndexOf(),search(),slice(),subString(),subStr(),valueOf().
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐