JavaScript 字符串(... 登录

JavaScript 字符串(String) 对象

JavaScript 字符串(String) 对象

String 对象用于处理已有的字符块。

如何使用长度属性来计算字符串的长度:

<html>
<body>
<script type="text/javascript">
var txt="Hello World!"
document.write(txt.length)
</script>
</body>
</html>

如何为字符串添加样式:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var txt="Hello World!"
document.write("<p>Big: " + txt.big() + "</p>")
document.write("<p>Small: " + txt.small() + "</p>")
document.write("<p>Bold: " + txt.bold() + "</p>")
document.write("<p>Italic: " + txt.italics() + "</p>")
document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>")
document.write("<p>Fixed: " + txt.fixed() + "</p>")
document.write("<p>Strike: " + txt.strike() + "</p>")
document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")
document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")
document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")
document.write("<p>Subscript: " + txt.sub() + "</p>")
document.write("<p>Superscript: " + txt.sup() + "</p>")
</script>
</body>
</html>

如何使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))
</script>
</body>
</html>

如何使用 match() 来查找字符串中特定的字符,并且如果找到的话,则返回这个字符:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var str="Hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!"))
</script>
</body>
</html>

如何使用 replace() 方法在字符串中用某些字符替换另一些字符:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/,"PHP中文网"))
</script>
</body>
</html>

字符串对象

字符串对象用于处理已有的字符块。

例子:

下面的例子使用字符串对象的长度属性来计算字符串的长度。

var txt="Hello world!"
document.write(txt.length)

上面的代码输出为:

12

下面的例子使用字符串对象的toUpperCase()方法将字符串转换为大写:

var txt="Hello world!"
document.write(txt.toUpperCase())

上面的代码输出为:

HELLO WORLD!

字符串使用strong>split()函数转为数组:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="demo">单击按钮显示数组。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
var str="a,b,c,d,e,f";
var n=str.split(",");
document.getElementById("demo").innerHTML=n[0];
}
</script>
</body>
</html>

特殊字符

Javascript 中可以使用反斜线(\)插入特殊符号,如:撇号,引号等其他特殊符号。

查看如下 JavaScript 代码:

var txt="We are the so-called "Vikings" from the north.";
document.write(txt);

在JavaScript中,字符串的开始和停止使用单引号或双引号。这意味着,上面的字符串将被切成: We are the so-called

解决以上的问题可以使用反斜线来转义引号:

var txt="We are the so-called \"Vikings\" from the north.";
document.write(txt);

JavaScript将输出正确的文本字符串:We are the so-called "Vikings" from the north.

下表列出其他特殊字符,可以使用反斜线转义特殊字符:

代码   输出

\'    单引号    

\"    双引号    

\\    斜杆    

\n    换行    

\r    回车    

\t    tab    

\b    空格    

\f    换页    


字符串属性和方法

属性:

length

prototype

constructor

方法:

charAt()

charCodeAt()

concat()

fromCharCode()

indexOf()

lastIndexOf()

match()

replace()

search()

slice()

split()

substr()

substring()

toLowerCase()

toUpperCase()

valueOf()


下一节
<html> <meta charset="utf-8"> <body> <script type="text/javascript"> var str="Hello world!" document.write(str.indexOf("Hello") + "<br />") document.write(str.indexOf("World") + "<br />") document.write(str.indexOf("world")) </script> </body> </html>
提交 重置代码
章节 评论 笔记 课件
  • 取消 回复 发送
  • 取消 发布笔记 发送