String类型的属性和内置方法

257 阅读3分钟

String类型的属性和内置方法

  1. 属性

length:返回字符串的字符长度,

constructor:返回创建 String 对象的函数,

prototype:返回创建 String 对象的函数

2.方法

- 字符方法

String 也包含对象的通用方法,比如 valueOf()、toLocaleString()和 toString()方法,但这 些方法都返回字符串的基本值。

charAt(n): 返回指定索引位置的字符,

charCodeAt(n):以 Unicode 编码形式返回指定索引位置的字符

var box = 'Mr.Lee';

alert(box.charAt(1)); //r

alert(box.charCodeAt(1)); //114

alert(box[1]); //r,通过数组方式截取

PS:box[1]在 IE 浏览器会显示 undefined,所以使用时要慎重。

- 字符串方法

concat(str1...str2):将字符串参数串联到调用该方法的字符串,

slice(n,m):返回字符串 n 到 m 之间位置的字符串,

substring(n,m):返回字符串 n 到 m 之间位置的字符串,

substr(n,m):返回字符串 n 开始的 m 个字符串


var box = 'Mr.Lee';

alert(box.concat(' is ', ' Teacher ', '!')); //Mr.Lee is Teacher !

alert(box.slice(3)); //Lee

alert(box.slice(3,5)); //Le

alert(box.substring(3)); //Lee

alert(box.substring(3,5)); //Le

alert(box.substr(3)); //Lee

alert(box.substr(3,5)); //Lee


var box = 'Mr.Lee';

alert(box.slice(-3));//Lee,6+(-3)=3 位开始

alert(box.substring(-3));//Mr.Lee 负数返回全部

alert(box.substr(-3));//Lee,6+(-3)=3 位开始


var box = 'Mr.Lee';

alert(box.slice(3, -1));//Le 6+(-1)=5, (3,5)

alert(box.substring(3, -1));//Mr. 第二参为负,直接转 0,并且方法会把较小的数字提前,(0,3)

alert(box.substr(3, -1));//'' 第二参数为负,直接转 0 ,(3,0)

PS:IE 的 JavaScript 实现在处理向 substr()方法传递负值的情况下存在问题,它会返回 原始字符串,使用时要切记。

indexOf(str, n):从 n 开始搜索的第一个 str,并将搜索的索引值返回,

lastIndexOf(str, n):从 n 开始搜索的最后一个 str,并将搜索的索引值返回

var box = 'Mr.Lee is Lee';
alert(box.indexOf('L'));//3
alert(box.indexOf('L', 5));//10
alert(box.lastIndexOf('L'));//10
alert(box.lastIndexOf('L', 5));//3,从指定的位置向前搜索

PS:如果没有找到想要的字符串,则返回-1。

示例:找出全部的 L
var box = 'Mr.Lee is Lee';//包含两个 L 的字符
var boxarr = [];//存放 L 位置的数
var pos = box.indexOf('L');//先获取第一个 L 的位
while (pos > -1) {//如果位置大于-1,说明还存在 
  boxarr.push(pos);//添加到数
  pos = box.indexOf('L', pos + 1);//从新赋值 pos 目前的位
}
alert(boxarr);//输出

toLowerCase(str):将字符串全部转换为小写,

toUpperCase(str):将字符串全部转换为大写,

toLocaleLowerCase(str):将字符串全部转换为大写,

toLocaleupperCase(str):将字符串全部转换为大写

PS:只有几种语言(如土耳其语)具有地方特有的大小写本地性,一般来说,是否本 地化效果都是一致的

match(pattern): 返回 pattern 中的子串或 null,

replace(pattern, replacement): 用 replacement 替换 pattern,

search(pattern): 返回字符串中 pattern 开始位置,

split(pattern): 返回字符串按指定 pattern 拆分的数组

PS: match()、replace()、serach()、split()在普通字符串中也可以使用。

var box = 'Mr.Lee is Lee';
alert(box.match('L')); //找到 L,返回 L 否则返回 null
alert(box.search('L')); //找到 L 的位置,和 indexOf 类型
alert(box.replace('L', 'Q')); //把 L 替换成 Q
alert(box.split(' ')); //以空格分割成字符串

fromCharCode(ascii): 静态方法,输出 Ascii 码对应值,

localeCompare(str1,str2): 比较两个字符串,并返回相应的值

localeCompare(str1,str2)方法详解:比较两个字符串并返回以下值中的一个;

1.如果字符串在字母表中应该排在字符串参数之前,则返回一个负数。(多数-1)

2.如果字符串等于字符串参数,则返回 0。

3.如果字符串在自附表中应该排在字符串参数之后,则返回一个正数。(多数 1)


var box = 'Lee';
alert(box.localeCompare('apple'));//1
alert(box.localeCompare('Lee'));//0
alert(box.localeCompare('zoo'));//-1

anchor(name):str,

big():str,

blink():str,

bold():Str,

fixed():Str,

fontcolor(color):str,

fontsize(size):str,

link(URL):str,

small():str,

strike():str,

italics():italics,

sub(): str,

sup(): str