indexof方法_JavaScript中indexOf()和search()方法之间的区别

news/2024/7/19 14:46:23 标签: python, java, 字符串, js, 正则表达式

indexof方法

Prerequisite:

先决条件:

  • JavaScript String indexOf() Method

    JavaScript字符串indexOf()方法

  • JavaScript String search() Method

    JavaScript字符串search()方法

Though indexOf() and search() both methods are used to check whether a substring exists in the string or not and returns either of the index of the substring or -1 (if substring does not exist in the string), there is a difference between indexOf() and search() methods. Consider the syntaxes of both methods:

尽管indexOf()和search()两种方法都用于检查字符串中是否存在子字符串,并返回子字符串的索引或-1(如果字符串中不存在子字符串), 则存在区别在indexOf()和search()方法之间 。 考虑这两种方法的语法:

Syntax of indexOf() method:

indexOf()方法的语法:

    String.indexOf(substring, [offset]);

Syntax of search() method:

search()方法的语法:

    String.search(substring);

Now, you can clearly see that in indexOf() method, there is an optional parameter(offset) from where we can start the searching but the method search() does not have this feature. It simply takes the substring and starts searching from the 0th index.

现在,您可以清楚地看到,在indexOf()方法中,有一个可选的参数(偏移),从此处可以开始搜索,但是方法search()没有此功能。 它只是获取子字符串并从第0 索引开始搜索。

Example:

例:

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
<script>		
var str = "friends say Hello";

var substr = "Hello";		
var index = str.indexOf(substr);
if(index!=-1)
	document.write(substr + " found at " + index + " position.<br>");
else
	document.write(substr + " does not exist in the " + str + ".<br>");

var substr = "Hello";		
var index = str.search(substr);
if(index!=-1)
	document.write(substr + " found at " + index + " position.<br>");
else
	document.write(substr + " does not exist in the " + str + ".<br>");

substr = "Hi";
index = str.indexOf(substr);		
if(index!=-1)
	document.write(substr + " found at " + index + " position.<br>");
else
	document.write(substr + " does not exist in the " + str + ".<br>");

substr = "Hi";
index = str.search(substr);		
if(index!=-1)
	document.write(substr + " found at " + index + " position.<br>");
else
	document.write(substr + " does not exist in the " + str + ".<br>");
</script>
</body>
</html>

Output

输出量

Hello found at 12 position.
Hello found at 12 position.
Hi does not exist in the friends say Hello.
Hi does not exist in the friends say Hello.


翻译自: https://www.includehelp.com/code-snippets/difference-between-indexOf-and-search-methods-in-javascript.aspx

indexof方法


http://www.niftyadmin.cn/n/1256387.html

相关文章

Linux怎么查找dtsi文件,在Linux dtsi文件中删除一个Node

介绍DTS(Device Tree Source)文件是对嵌入式设备硬件信息的描述。因为一个SoC可能对应多个Machine(一个SoC能够对应多个产品和电路板)&#xff0c;势必这些.dts文件需包含许多共同的部分&#xff0c;根据软件设计中“抽象”的思想。Linux把SoC公用的部分或者多个machine共同的部…

c#算术运算符_设计Windows应用程序以在C#中执行算术运算

c#算术运算符Here, we demonstrate basic arithmetic operations in C# Windows Application. It is very useful application to understand the basic concepts of a Windows Application in using C#. 在这里&#xff0c;我们演示C&#xff03;Windows应用程序中的基本算术运…

linux文件删除漏洞,Piwigo任意文件泄露和任意文件删除漏洞

发布日期&#xff1a;2013-02-18更新日期&#xff1a;2013-02-21受影响系统&#xff1a;Piwigo Piwigo描述&#xff1a;--------------------------------------------------------------------------------BUGTRAQ ID: 58016Piwigo是用PHP编写的相册脚本。Piwigo 2.4.6及其他…

Python程序从给定的整数列表中打印完美数字

Given a list of the integer numbers and we have to print all perfect numbers present in the given list. 给定一个整数列表&#xff0c;我们必须打印给定列表中存在的所有正整数。 This Program iterating through each number one by one in the list, and check wheth…

树莓派2 安装linux系统安装教程,Raspberry Pi 2安装kali Linux系统

工具* Raspberry Pi 2* sd卡和读卡器* 网线* PC电脑方法/步骤0x01. 下载工具和安装Kali镜像Win32DiskImager(系统烧录工具)SDFormatter (sd卡格式化工具)kali镜像(官网)这里建议用迅雷下载rpi2&#xff0c;然后解压PuTTY(ssh登录工具)vncviewer(客户端)工具0x02. 做好了以上的…

armstrong公理系统_Python程序使用面向对象的方法检查Armstrong号码

armstrong公理系统Armstrong Number - An Armstrong Number is a Number which is equal to its sum of digits cube. For example - 153 is an Armstrong number: here 153 (1*1*1) (5*5*5) (3*3*3). 阿姆斯壮数字 -阿姆斯壮数字是一个数字&#xff0c;等于它的数字和。 例…

计算给定N的从1到N的所有数字的数字总和

Problem statement: We have given a number N and we have to find out the sum of digits in all numbers from 1 to N. 问题陈述&#xff1a;我们给了一个数字N &#xff0c;我们必须找出从1到N的所有数字的数字总和 。 Solution 解 Brute force approach: 蛮力法&#xf…

linux zcat 使用方法,每天学一个 Linux 命令:zcat

命令简介zcat 命令用于显示压缩包中文件的内容&#xff0c;可以使用 gzip -d 或 gunzip 或 zcat 将压缩文件恢复为原始格式。zcat 与 gunzip -c 相同。zcat 命令用于不真正解压缩文件&#xff0c;就能显示压缩包中文件的内容的场合。语法格式zcat [ -fhLV ] [ name ... ]选项说…