当前位置: 首页 > news >正文

JavaScript String 字符串对象实例合集

文章目录

  • JavaScript String 字符串对象实例合集
    • 返回字符串的长度
    • 为字符串添加样式
    • 返回字符串中指定文本首次出现的位置 - indexOf()方法
    • 查找字符串中特定的字符,若找到,则返回该字符 - match() 方法
    • 替换字符串中的字符 - replace()


JavaScript String 字符串对象实例合集

在这里插入图片描述

返回字符串的长度

  • 源码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>梁辰兴实例</title>
</head>
<body>
<script>
var txt = "Hello World!";
document.write(txt.length);
</script>
</body>
</html>
  • 运行效果
    在这里插入图片描述

为字符串添加样式

  • 源码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>梁辰兴实例</title>
</head>
<body>
<script>
var txt = "Hello World!";
document.write("<p>字体变大: " + txt.big() + "</p>");
document.write("<p>字体缩小: " + txt.small() + "</p>");
document.write("<p>字体加粗: " + txt.bold() + "</p>");
document.write("<p>斜体: " + txt.italics() + "</p>");
document.write("<p>固定定位: " + txt.fixed() + "</p>");
document.write("<p>加删除线: " + txt.strike() + "</p>");
document.write("<p>字体颜色: " + txt.fontcolor("green") + "</p>");
document.write("<p>字体大小: " + txt.fontsize(6) + "</p>");
document.write("<p>下标: " + txt.sub() + "</p>");
document.write("<p>上标: " + txt.sup() + "</p>");
document.write("<p>链接: " + txt.link("http://www.") + "</p>");
document.write("<p>闪动文本: " + txt.blink() + " (不能用于IE,Chrome,或者Safari)</p>");
</script>
</body>
</html>
  • 运行效果
    在这里插入图片描述

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

  • 源码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>梁辰兴实例</title>
</head>
<body>
<p id="demo">单击按钮来定位指定文本首次出现的位置。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){var str="Hello world, welcome to the universe.";var n=str.indexOf("welcome");document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
  • 运行效果
    在这里插入图片描述
    在这里插入图片描述

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

  • 源码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>梁辰兴实例</title>
</head>
<body>
<script>
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()

  • 源码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>梁辰兴实例</title>
</head>
<body>
<p>单击按钮将段落中“Microsoft”替换成“W3CSchool”:</p>
<p id="demo">Visit Microsoft!</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){var str=document.getElementById("demo").innerHTML; var n=str.replace("Microsoft","W3CSchool");document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
  • 运行效果
    在这里插入图片描述
    在这里插入图片描述
http://www.lryc.cn/news/37342.html

相关文章:

  • 实习生培养计划
  • 【服务器管理】Wordpress服务器内存占用太高(优化方案详解)
  • 【ECCV 2022】76小时动捕,最大规模数字人多模态数据集开源
  • 联合解决方案 | 亚信科技AntDB数据库携手浪潮K1 Power赋能关键行业数字化转型,助力新基建
  • Android 单元测试问题总结(Robolectric+JUnit)
  • 专项攻克——二叉树
  • PACS系统源码 PACS源码 三维重建PACS源码
  • 利用Mysql存储过程造百万级数据
  • Vue2组件之间的传值通信
  • Spring Boot官方例子《Developing Your First Spring Boot Application》无法运行
  • 数据结构(3)— 线性表之顺序存储详解介绍(含代码)
  • ChatGPT正当时,让我们一起深耕智能内容生成和智能内容增强领域
  • 天梯赛训练L1-019 (谁先倒)
  • MySQL DQL语句基础(一)
  • ccc-pytorch-LSTM(8)
  • 教育小程序开发解决方案
  • 动态规划之股票问题大总结
  • 我来跟你讲vue进阶
  • #847(Div3)E. Vlad and a Pair of Numbers
  • 怎么把pdf转换成图片?这个方法你值得拥有
  • go语言使用append向二维数组添加一维数组
  • YOLOv5训练大规模的遥感实例分割数据集 iSAID从切图到数据集制作及训练
  • js学习5(函数)
  • 用Qt画一个仪表盘
  • linux 端口查询命令
  • C语言函数: 字符串函数及模拟实现strtok()、strstr()、strerror()
  • 【学习笔记】人工智能哲学研究:《心智、语言和机器》
  • 设计模式之门面模式(外观模式)
  • MySQL - 多表查询
  • 自定义报表是什么?