zoukankan      html  css  js  c++  java
  • 返回sourceString 中出现的第一个 searchString 的索引

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <meta name="author" content="杨欣">
        <title>返回 sourceString 中出现的第一个 searchString 的索引</title>
        <style>
    
        </style>
    </head>
    
    <body>
    
        <script>
            /**
            “假设有两个字符串 sourceString 和 searchString,要求你返回
             sourceString 中出现的第一个 searchString 的索引。
             如果 sourceString 中不包含 searchString,则返回 -1。”
            */
    
            function searchIndex(searchString,sourceString){
                for (let i = 0; i < sourceString.length; i++) {
                    let possibleMatch=sourceString.substr(i,searchString.length);
                    if(possibleMatch==searchString){
                        return i
                    }
                } 
                return -1
            }
            console.log(searchIndex('de','abcdeddef'))
        </script>
    </body>
    
    </html>
    

      

  • 相关阅读:
    drf项目部署到腾讯云
    ruby的DIR.pwd
    ruby+selenium-webdriver测试
    ruby
    ruby类对象和对象
    ruby的实例变量
    ruby在类中访问@,类外访问调用方法
    ruby中=>是什么意思
    ruby
    css content属性
  • 原文地址:https://www.cnblogs.com/samsara-yx/p/10514369.html
Copyright © 2011-2022 走看看