- 字符串
1.内置对象
在JavaScript中已经定义好的对象
比较常用的 JavaScript 内置对象主要有以下几种:
(1)字符串对象 String;
(2)日期对象 Date;
(3)数组对象 Array;
(4)数值对象 Math 和 Number;
2.字符串属性 : length
可以用来获取字符串的长度
- 处理字符串的方法(11种)
(1)match():查询字符串中字符串是否存在,存在就输出
var str1='java c++ c# python html css';
alert(str1.length); 获取字符串的长度
document.write(str1.match('python'));
返回null值: 表示没有查询到love
document.write(str1.match('Python'));
(2)search():检索字符串中字符所在的索引,返回索引值
document.write(str1.search('Sc'));
(3)indexOf():返回元素在在字符串中第一次出现的索引
document.write(str1.indexOf('a'));
(4)replace():在字符串中用一些字符替换另外一些字符
document.write(str1.replace('JavaScript','InLett'));
(5)charAt():获取字符串中制定字符
document.write(str1.charAt(3));
(6)大小写转换:tolowerCase()和toUpperCase()
document.write(str1.toLowerCase());
document.write(str1.toUpperCase());
(7)concat():链接字符串
document.write(str1.concat(',HTML',',CSS'));
(8)比较字符串localeCompare(): 根据ASCII表的排列进行比较
var str2='java c++ c# python html css';
document.write(str2.localeCompare('avaScript'));
(9)split():分割字符串(分割符是以字符串中的字符进行分割) 返回一个数组
var arr=new Array();
arr=str2.split(',');
document.write(arr);
document.write(arr[0]);
document.write(arr[1]);
(10)subString(start,end):截取字符串
var str3='java c++ c# python html css';
不包含结束的索引对应的值
document.write(str3.substring(0,2));
document.write(str3.substring(3,38));
document.write(str3.substring(4,2));