JavaScript String(字符串)对象的简单实例(推荐)

前端之家收集整理的这篇文章主要介绍了JavaScript String(字符串)对象的简单实例(推荐)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

返回字符串的长度:

<script type="text/javascript">

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

为字符串添加样式:

<script type="text/javascript">

var txt="Hello World!"

document.write("

Big: " + txt.big() + "

")
document.write("

Small: " + txt.small() + "

")

document.write("

Bold: " + txt.bold() + "

")
document.write("

Italic: " + txt.italics() + "

")

document.write("

Blink: " + txt.blink() + " (does not work in IE)

")
document.write("

Fixed: " + txt.fixed() + "

")
document.write("

Strike: " + txt.strike() + "

")

document.write("

Fontcolor: " + txt.fontcolor("Red") + "

")
document.write("

Fontsize: " + txt.fontsize(16) + "

")

document.write("

Lowercase: " + txt.toLowerCase() + "

")
document.write("

Uppercase: " + txt.toUpperCase() + "

")

document.write("

Subscript: " + txt.sub() + "

")
document.write("

Superscript: " + txt.sup() + "

")

document.write("

Link: " + txt.link("http://www.w3school.com.cn") + "

")

返回字符串中指定文本首次出现的位置 - indexOf()方法

<script type="text/javascript">

var str="Hello world!"
document.write(str.indexOf("Hello") + "
")
document.write(str.indexOf("World") + "
")
document.write(str.indexOf("world"))

效果如下:

查找字符串中特定的字符,若找到,则返回该字符 - match() 方法

<script type="text/javascript">

var str="Hello world!"
document.write(str.match("world") + "
")
document.write(str.match("World") + "
")
document.write(str.match("worlld") + "
")
document.write(str.match("world!"))

效果如下:

替换字符串中的字符 - replace():

<script type="text/javascript">

var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/,"W3School"))

效果如下:

以上这篇JavaScript String(字符串)对象的简单实例(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

原文链接:https://www.f2er.com/js/46196.html

猜你在找的JavaScript相关文章