平凉市城乡建设局网站,黑色门户网站源码,seo算法是什么,山东seo推广公司文章目录JavaScript String 字符串对象JavaScript 字符串字符串#xff08;String#xff09;在字符串中查找字符串内容匹配替换内容字符串大小写转换字符串转为数组特殊字符字符串属性和方法JavaScript String 字符串对象 String 对象用于处理已有的字符块。
JavaScript 字…
文章目录JavaScript String 字符串对象JavaScript 字符串字符串String在字符串中查找字符串内容匹配替换内容字符串大小写转换字符串转为数组特殊字符字符串属性和方法JavaScript String 字符串对象 String 对象用于处理已有的字符块。
JavaScript 字符串
一个字符串用于存储一系列字符就像 “John Doe”.
一个字符串可以使用单引号或双引号
var carnameVolvo XC60;
var carnameVolvo XC60;你使用位置索引可以访问字符串中任何的字符
var charactercarname[7];字符串的索引从零开始, 所以字符串第一字符为 [0],第二个字符为 [1], 等等。
你可以在字符串中使用引号如下实例
实例
var answerIts alright;
var answerHe is called Johnny;
var answerHe is called Johnny;或者你可以在字符串中使用转义字符()使用引号
实例
var answerIt\s alright;
var answerHe is called \Johnny\;字符串String
字符串String使用长度属性length来计算字符串的长度
实例
var txtHello World!;
document.write(txt.length);
var txtABCDEFGHIJKLMNOPQRSTUVWXYZ;
document.write(txt.length);在字符串中查找字符串
字符串使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置
实例
var strHello world, welcome to the universe.;
var nstr.indexOf(welcome);如果没找到对应的字符函数返回-1
lastIndexOf() 方法在字符串末尾开始查找字符串出现的位置。
内容匹配
match() 函数用来查找字符串中特定的字符并且如果找到的话则返回这个字符。
实例
var strHello world!;
document.write(str.match(world) br);
document.write(str.match(World) br);
document.write(str.match(world!));替换内容
replace() 方法在字符串中用某些字符替换另一些字符。
实例
strPlease visit Microsoft!
var nstr.replace(Microsoft,w3cschool);字符串大小写转换
字符串大小写转换使用函数 toUpperCase() / toLowerCase():
实例
var txtHello World!; // String
var txt1txt.toUpperCase(); // txt1 文本会转换为大写
var txt2txt.toLowerCase(); // txt2 文本会转换为小写字符串转为数组
字符串使用split()函数转为数组:
实例
txta,b,c,d,e // String
txt.split(,); // 使用逗号分隔
txt.split( ); // 使用空格分隔
txt.split(|); // 使用竖线分隔 特殊字符
Javascript 中可以使用反斜线\插入特殊符号如撇号,引号等其他特殊符号。
查看如下 JavaScript 代码:
var txtWe are the so-called Vikings from the north.;
document.write(txt);在JavaScript中字符串的开始和停止使用单引号或双引号。这意味着上面的字符串将被切成
We are the so-called
解决以上的问题可以使用反斜线来转义引号
var txtWe are the so-called \Vikings\ from the north.;
document.write(txt);JavaScript将输出正确的文本字符串We are the so-called “Vikings” from the north.
下表列出其他特殊字符可以使用反斜线转义特殊字符
代码输出’单引号双引号| 斜杆\n换行\r回车\ttab\b空格\f换页
字符串属性和方法
属性:
lengthprototypeconstructor
方法:
charAt()charCodeAt()concat()fromCharCode()indexOf()lastIndexOf()match()replace()search()slice()split()substr()substring()toLowerCase()toUpperCase()valueOf()