属性 | 描述 |
length | 在大多数情况下返回字符串中的字符数 |
方法 | 描述 |
toUpperCase() | 将字符串修改为大写字母 |
toLowerCase() | 将字符串修改为小写字母 |
charAt() | 以索引编号为参数,返回这个位置的字符 |
indexOf() | 在字符串中查找一个或一组字符,返回首次出现的索引编号 |
lastIndexOf() | 在字符串中查找一个或一组字符,返回最后一次出现的索引编号 |
substring() | 返回两个索引编号之间的字符,包含首索引编号位置的字符,不包含第二个索编引号位置的字符 |
split() | 当指定一个字符时,它用查找到的每个此字符将字符串分割,然后将它们存储在一个数组中 |
trim() | 删除字符串开始和结尾的空格 |
replace() | 有些像查找与替换,它有一个需要查找的值和一个用于替换的值(默认情况下,它只替换第一个查找到的项) |
String 对象的属性和方法用于操作字符串。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
title
>Title</
title
>
</
head
>
<
body
>
<
script
>
var str="I like javascript ";
document.write(str);
document.write('<
br
>');
// 属性长度
document.write(str.length);
document.write('<
br
>');
// 转大写
document.write(str.toUpperCase());
document.write('<
br
>');
// 转小写
document.write(str.toLowerCase());
document.write('<
br
>');
// 返回指定位置的字符,不包括空
document.write(str.charAt(5));
document.write('<
br
>');
// 返回字符的位置
document.write(str.indexOf('a'));
document.write('<
br
>');
// 返回字符最后一次出现的位置
document.write(str.lastIndexOf('a'));
document.write('<
br
>');
// 从字符串中取指定范围的字符,从开始,包括空格
document.write(str.substring(0,4));
document.write('<
br
>');
// 将字符串按分解规则分解成数组
var value=str.split(" ");
document.write(value[0]);
document.write('<
br
>');
// 去年字符串开始和结尾的空格
document.write(str.trim());
document.write('<
br
>');
// 查看和替换
document.write(str.replace('javascript','C++'));
document.write('<
br
>');
</
script
>
</
body
>
</
html
>
|