全网最详细的js判断字符串包含某个字符串的多种方法,比如search(),includes(),indexOf(),lastIndexOf()等

1. 引言

在运行如下代码之前,你需要下载如下文件:

  1. bootstrap.min.css

  2. jquery-3.4.1.js

并且分别创建 字符串查找.html文件和字符串查询.js文件。

2. string.search()

  1. 用法:string.search(searchString)

    • string表示原字符串

    • searchString表示待查找的字符串

  2. 返回值:number

    • 匹配成功的第一个字符下标

    • 未匹配则返回-1

  3. 字符串查找.html源代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查询字符串</title>
    <link href="../bootstrap.min.css" rel="stylesheet">
    <link href="../normalize.css" rel="stylesheet">
</head>
<body>
<div style="margin:10px;padding: 10px;">
    <form class="form-horizontal col-sm-5 " role="form">
        <div class="form-group">
            <label for="sourceString" class="col-sm-2 control-label">原字符串:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="sourceString" placeholder="请输入原字符串">
            </div>
        </div>
        <div class="form-group">
            <label for="waitSearchString" class="col-sm-2 control-label">查询字符:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="waitSearchString" placeholder="请输入查询字符">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">查询结果:</label>
            <div class="col-sm-10">
                <span type="text" class="form-control" style="color: red" id="searchResult" readonly></span>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" class="btn btn-default" onclick="search()">点击查询</button>
            </div>
        </div>
    </form>
</div>

<script src="../../js/jquery-3.4.1.js"></script>
<script src="字符串查询.js"></script>
</body>
</html>
  1. 字符串查询.js源代码
const search = function () {
    //原字符串
    let sourceString = $("#sourceString").val();
    if (!sourceString) {
        $("#searchResult").html("原字符串不能为空")
        return;
    }

    //查询字符
    let waitSearchString = $("#waitSearchString").val();
    if (!waitSearchString) {
        $("#searchResult").html("查询字符不能为空")
        return;
    }

    //判断原字符串是否包含查询字符
    $("#searchResult").html(sourceString.search(waitSearchString));
}
  1. 测试结果
  • 原字符串为空时的测试

在这里插入图片描述

  • 返回所在字符串你好呀中的下标,即0,因为下标是从0开始的

在这里插入图片描述

  • 返回所在字符串你好呀中的下标,即2

在这里插入图片描述

3. string.includes()

  1. 用法:string.includes(searchString, start)

    • string表示原字符串

    • searchString 表示待查找的字符串

    • start 表示指定下标

  2. 返回值:boolean

    • true表示匹配成功

    • false表示匹配失败

  3. 字符串查找.html源代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查询字符串</title>
    <link href="../bootstrap.min.css" rel="stylesheet">
    <link href="../normalize.css" rel="stylesheet">
</head>
<body>
<div style="margin:10px;padding: 10px;">
    <form class="form-horizontal col-sm-5 " role="form">
        <div class="form-group">
            <label for="sourceString" class="col-sm-2 control-label">原字符串:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="sourceString" placeholder="请输入原字符串">
            </div>
        </div>
        <div class="form-group">
            <label for="waitSearchString" class="col-sm-2 control-label">查询字符:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="waitSearchString" placeholder="请输入查询字符">
            </div>
        </div>
        <div class="form-group">
            <label for="position" class="col-sm-2 control-label">指定下标:</label>
            <div class="col-sm-10">
                <input type="number" min="0" class="form-control" id="position" placeholder="请输入指定下标">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">查询结果:</label>
            <div class="col-sm-10">
                <span type="text" class="form-control" style="color: red" id="searchResult" readonly></span>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" class="btn btn-default" onclick="includes()">点击查询</button>
            </div>
        </div>
    </form>
</div>

<script src="../../js/jquery-3.4.1.js"></script>
<script src="字符串查询.js"></script>
</body>
</html>
  1. 字符串查询.js源代码
const includes = function () {
    //原字符串
    let sourceString = $("#sourceString").val();
    if (!sourceString) {
        $("#searchResult").html("原字符串不能为空")
        return;
    }

    //查询字符
    let waitSearchString = $("#waitSearchString").val();
    if (!waitSearchString) {
        $("#searchResult").html("查询字符不能为空")
        return;
    }

    //获取指定下标
    let position = $("#position").val();
    if (!position) {
        $("#searchResult").html("指定下标不能为空")
        return;
    }

    //判断原字符串是否包含查询字符
    $("#searchResult").html(sourceString.includes(waitSearchString, position).toString());
}
  1. 测试结果
  • 指定下标为空

在这里插入图片描述

  • 指定下标不超过原字符串的下标长度

在这里插入图片描述

  • 指定下标超过原字符串的下标长度

在这里插入图片描述

4. string.indexOf()

  1. 用法:string.indexOf(searchString, start)

    • string表示原字符串

    • searchString 表示待查找的字符串

    • start 表示指定下标

  2. 返回值:number

    • 匹配成功后的第一个字符下标

    • 未匹配则返回-1

  3. 字符串查找.html源代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查询字符串</title>
    <link href="../bootstrap.min.css" rel="stylesheet">
    <link href="../normalize.css" rel="stylesheet">
</head>
<body>
<div style="margin:10px;padding: 10px;">
    <form class="form-horizontal col-sm-5 " role="form">
        <div class="form-group">
            <label for="sourceString" class="col-sm-2 control-label">原字符串:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="sourceString" placeholder="请输入原字符串">
            </div>
        </div>
        <div class="form-group">
            <label for="waitSearchString" class="col-sm-2 control-label">查询字符:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="waitSearchString" placeholder="请输入查询字符">
            </div>
        </div>
        <div class="form-group">
            <label for="position" class="col-sm-2 control-label">指定下标:</label>
            <div class="col-sm-10">
                <input type="number" min="0" class="form-control" id="position" placeholder="请输入指定下标">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">查询结果:</label>
            <div class="col-sm-10">
                <span type="text" class="form-control" style="color: red" id="searchResult" readonly></span>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" class="btn btn-default" onclick="indexOf()">点击查询</button>
            </div>
        </div>
    </form>
</div>

<script src="../../js/jquery-3.4.1.js"></script>
<script src="字符串查询.js"></script>
</body>
</html>
  1. 字符串查询.js源代码
const indexOf = function () {
    //原字符串
    let sourceString = $("#sourceString").val();
    if (!sourceString) {
        $("#searchResult").html("原字符串不能为空")
        return;
    }

    //查询字符
    let waitSearchString = $("#waitSearchString").val();
    if (!waitSearchString) {
        $("#searchResult").html("查询字符不能为空")
        return;
    }

    //获取指定下标
    let position = $("#position").val();
    if (!position) {
        $("#searchResult").html("指定下标不能为空")
        return;
    }

    //判断原字符串是否包含查询字符
    $("#searchResult").html(sourceString.indexOf(waitSearchString, position));
}
  1. 测试结果
  • 查询字符为空

在这里插入图片描述

  • 指定下标不超过原字符串的下标长度

在这里插入图片描述

  • 指定下标超过原字符串的下标长度

在这里插入图片描述

5. string.lastIndexOf()

  1. 用法:string.indexOf(searchString, start),从字符串的尾部开始查找

    • string表示原字符串

    • searchString 表示待查找的字符串

    • start 表示指定下标

  2. 返回值:number

    • 匹配成功后的第一个字符下标

    • 未匹配则返回-1

  3. 字符串查找.html源代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查询字符串</title>
    <link href="../bootstrap.min.css" rel="stylesheet">
    <link href="../normalize.css" rel="stylesheet">
</head>
<body>
<div style="margin:10px;padding: 10px;">
    <form class="form-horizontal col-sm-5 " role="form">
        <div class="form-group">
            <label for="sourceString" class="col-sm-2 control-label">原字符串:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="sourceString" placeholder="请输入原字符串">
            </div>
        </div>
        <div class="form-group">
            <label for="waitSearchString" class="col-sm-2 control-label">查询字符:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="waitSearchString" placeholder="请输入查询字符">
            </div>
        </div>
        <div class="form-group">
            <label for="position" class="col-sm-2 control-label">指定下标:</label>
            <div class="col-sm-10">
                <input type="number" min="0" class="form-control" id="position" placeholder="请输入指定下标">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">查询结果:</label>
            <div class="col-sm-10">
                <span type="text" class="form-control" style="color: red" id="searchResult" readonly></span>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" class="btn btn-default" onclick="lastIndexOf()">点击查询</button>
            </div>
        </div>
    </form>
</div>

<script src="../../js/jquery-3.4.1.js"></script>
<script src="字符串查询.js"></script>
</body>
</html>
  1. 字符串查询.js源代码
const lastIndexOf = function () {
    //原字符串
    let sourceString = $("#sourceString").val();
    if (!sourceString) {
        $("#searchResult").html("原字符串不能为空")
        return;
    }

    //查询字符
    let waitSearchString = $("#waitSearchString").val();
    if (!waitSearchString) {
        $("#searchResult").html("查询字符不能为空")
        return;
    }

    //获取指定下标
    let position = $("#position").val();
    if (!position) {
        $("#searchResult").html("指定下标不能为空")
        return;
    }

    //判断原字符串是否包含查询字符
    $("#searchResult").html(sourceString.lastIndexOf(waitSearchString, position));
}
  1. 测试结果
  • 指定下标2,表示从位置往前查找,自然能查到
    在这里插入图片描述

  • 指定下标1,表示从位置往前查找,自然查不到

在这里插入图片描述

6. 总结

以上四种方式查询字符串,根据你的需求来使用。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你好!关于华为随行WiFi的前后端联调,首先需要明确一下具体的配置内容。通常来说,前端和后端的联调是为了确保系统的功能能够正常运行,并且前后端之间的数据交互能够顺利进行。 在华为随行WiFi的配置过程中,前端主要负责用户界面的展示和用户交互,而后端则负责处理用户请求,并与数据库或其他服务进行交互。为了进行前后端联调,你可以按照以下步骤进行: 1. 确保前后端开发环境已经搭建好,并且能够正常运行。 2. 在开发环境中,将前端和后端的代码进行整合,确保两者能够正常编译和启动。 3. 针对具体的功能模块,定义好前后端之间的接口。 4. 在前端代码中调用后端接口,发送请求并处理返回的数据。 5. 在后端代码中实现相应的接口,处理前端发送的请求,并返回相应的数据。 6. 运行前端和后端代码,通过接口进行数据交互。 7. 针对每个接口,进行测试和调试,确保数据的传输和处理逻辑正确无误。 8. 如果发现问题或错误,及时进行修改和调试,直到联调过程顺利完成。 在进行前后端联调过程中,可以借助一些调试工具和技术,例如使用浏览器的开发者工具来检查网络请求和响应,使用接口测试工具来模拟前端发送请求等。此外,注意及时记录和解决遇到的问题,保持前后端的沟通和协作,有助于提高联调的效率和质量。 希望以上信息对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网全栈开发实战

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值