Heim  >  Artikel  >  Web-Frontend  >  JavaScript字符串处理(String对象)详解_基础知识

JavaScript字符串处理(String对象)详解_基础知识

WBOY
WBOYOriginal
2016-05-16 16:33:221388Durchsuche

定义字符串(String)对象

JavaScript String 对象用于处理文本字符串。创建 String 对象语法如下:

复制代码 代码如下:



以上三种方法中,只有第一种是使用 String 构造函数严格的定义一个字符串对象,返回的也是一个对象(object)。第二种是调用 String 函数,将转换参数 str 为原始字符串字符串并返回。第三种是定义一个字符串变量,但在 JavaScript 仍然按照字符串对象来处理。

运行下面的语句便可知道它们的区别:

复制代码 代码如下:

alert( typeof str_object ); // 输出 object
alert( typeof str1 );  // 输出 string
alert( typeof str2 );  // 输出 string

String 对象属性

属性 描述
constructor 对创建该对象的函数的引用
length 字符串的长度
prototype 向对象添加属性和方法

String 对象方法

String 对象常用方法列表如下:

字符串输出

alert():消息警告框输出文字
document.write():向 Web 页面输出文字

字符串操作

concat():连接两个或多个字符串
replace():字符串替换或正则匹配替换
slice():通过指定开始和结束位置来截取字符串
split():将字符串分割为字符串数组
substr():根据开始位置和长度截取字符串
substring():通过指定开始和结束位置来截取字符串

字符串转换

toLowerCase():把字符串转换为小写
toUpperCase():把字符串转换为大写
fromCharCode():将一个或多个 Unicode 值转换为字符串

字符串查找

charAt():取得指定位置的字符
charCodeAt():取得指定位置字符的 Unicode 编码
indexOf():计算某个指定的字符串在字符串中首次出现的位置
lastIndexOf():计算某个指定的字符串在字符串中首次出现的位置

HTML标签类

bold():取得指定位置的字符
fontcolor():按照指定的颜色来显示字符串
fontsize():按照指定的尺寸来显示字符串
italics():把字符串显示为斜体
link():给字符串加上超链接
strike():给字符串加上删除线
sub():把字符串显示为下标
sup():把字符串显示为上标

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn