如何在 JavaScript 中获取字符串中指定值的第一个索引?

javascriptweb developmentfront end technology

要获取字符串中指定值的第一个索引,请使用 JavaScript indexOf() 方法。

示例

您可以尝试运行以下代码来获取第一个索引 −

<html>
   <head>
      <title>JavaScript String indexOf() Method</title>
   </head>

   <body>
      <script>
         var str = new String( "Learning is fun! Learning is sharing!" );
         var index = str.indexOf( "Learning" );
         document.write("First index of string Learning :" + index );
      </script>
   </body>
</html>

相关文章


有用资源