Java中indexOf()方法詳解及其日常使用舉例
摘要:
indexOf()
方法用于在字符串中查找指定子串,并返回第一個匹配項的索引。Java 提供了四種常用的查找方法,分別是 indexOf(String str)
、indexOf(String str, int startIndex)
、lastIndexOf(String str)
、lastIndexOf(String str, int startIndex)
。
Demo 理解:
上面的示例代碼展示了在字符串中使用 indexOf()
方法進行查找的示例。通過設(shè)置不同的起始位置,可以靈活地定位子串。
Java 中的 indexOf() 方法: ??
indexOf()
方法用于在字符串中查找指定子串,并返回第一個匹配項的索引。它從指定的字符位置開始搜索,檢查指定數(shù)量的字符位置。
在 Java 中,共有四種常用的查找方法:
int indexOf(String str)
: 返回第一次出現(xiàn)的指定子字符串在此字符串中的索引。int indexOf(String str, int startIndex)
: 從指定的索引處開始,返回第一次出現(xiàn)的指定子字符串在此字符串中的索引。int lastIndexOf(String str)
: 返回在此字符串中最右邊出現(xiàn)的指定子字符串的索引。int lastIndexOf(String str, int startIndex)
:此方法接收兩個參數(shù),第一個參數(shù)是要查找的子字符串str
,第二個參數(shù)是開始搜索的索引位置startIndex
。它會從指定的索引位置向后搜索,并返回最后一次出現(xiàn)指定子字符串的索引位置。如果在指定的索引位置之后沒有找到子字符串,則返回 -1。如果子字符串為空字符串或者搜索索引超出了字符串的長度,則返回字符串的長度。
以下是一個示例代碼:
String str = "Hello, World!"; int index = str.lastIndexOf("o", 7); System.out.println(index); // 輸出4
在以上示例代碼中,我們首先創(chuàng)建了一個字符串 str
,然后調(diào)用了 lastIndexOf
方法來查找最后一次出現(xiàn)字符 o
的位置,在字符串 str
中從索引 0 到索引 7 的范圍內(nèi)進行查找,即 "Hello, ",結(jié)果為 4。
indexOf
使用示例:
package com.example.democrud.democurd.controller; public class IndexOfExample { public static void main(String[] args) { String s = "yanwenchaoyan"; // 從頭開始查找是否存在指定的字符 // 不存在則返回 -1, 存在則返回對應(yīng)的下標(biāo)位置 int le = s.indexOf("l"); int les = s.indexOf("n"); System.out.println("yanwenchao 的不存在則為:" + le); System.out.println("yanwenchao 的存在的下標(biāo)為:" + les); System.out.println("---------------------------------------"); // 從第四個字符位置開始往后繼續(xù)查找,包含當(dāng)前位置,前面的就過濾掉 int i = s.indexOf("y", 3); System.out.println("yanwenchao 的下標(biāo)位置是:" + i); System.out.println("---------------------------------------"); String s1 = "01234560123456"; int ls = s1.indexOf("123"); int lss = s1.lastIndexOf("123"); //反方向 int lsss = s1.lastIndexOf("123", 4); System.out.println("下標(biāo)地址:" + ls); System.out.println("下標(biāo)地址:" + lss); System.out.println("下標(biāo)地址:" + lsss); System.out.println("---------------------------------------"); } }
運行結(jié)果:
yanwenchao 的不存在則為:-1
yanwenchao 的存在的下標(biāo)為:2
---------------------------------------
yanwenchao 的下標(biāo)位置是:10
---------------------------------------
下標(biāo)地址:1
下標(biāo)地址:8
下標(biāo)地址:1
---------------------------------------
注意:指定了索引位置之后,將從指定索引位置開始進行查詢,返回相應(yīng)的下標(biāo)值。例如,如果搜索字符 “y”,則返回字符 “y” 第一次出現(xiàn)的位置。
獲取動態(tài)數(shù)組元素的索引:
如果我們想獲得最后一次出現(xiàn) “Runoob” 的位置,我們可以使用 lastIndexOf()
方法。
public static void main(String[] args){ // 創(chuàng)建一個數(shù)組 ArrayList<String> sites = new ArrayList<>(); sites.add("Google"); sites.add("Runoob"); sites.add("Taobao"); System.out.println("網(wǎng)站列表: " + sites); // 查找位置索引值為 "Runoob" 的元素 // 如果存在,則返回對應(yīng)的下標(biāo)位置(下標(biāo)從 0 開始) // 例如:Google 對應(yīng)下標(biāo) 0, Runoob 對應(yīng)下標(biāo) 1, Taobao 對應(yīng)下標(biāo) 2 // 所以返回 1 int position1 = sites.indexOf("Runoob"); System.out.println("Runoob 的索引位置: " + position1); // 查找位置索引值為 "Weibo" 的元素 // 不存在,則返回 -1 int position2 = sites.indexOf("Weibo"); System.out.println("Weibo 的索引位置: " + position2); }
運行結(jié)果:
網(wǎng)站列表: [Google, Runoob, Taobao]
Runoob 的索引位置: 1
Weibo 的索引位置: -1
lastIndexOf 方法
lastIndexOf()
方法返回指定元素在動態(tài)數(shù)組中最后一次出現(xiàn)的位置。
public static void main(String[] args){ // 創(chuàng)建一個數(shù)組 ArrayList<String> sites = new ArrayList<>(); // "Runoob" 存在兩次,下面代碼會得到最后一次出現(xiàn)的位置 sites.add("Google"); sites.add("Runoob"); sites.add("Taobao"); sites.add("Runoob"); System.out.println("網(wǎng)站列表: " + sites); // 獲取 "Runoob" 最后一次出現(xiàn)的位置 int position1 = sites.lastIndexOf("Runoob"); System.out.println("Runoob 最后出現(xiàn)的位置: " + position1); // "Wiki" 不在數(shù)組中,返回 -1 int position2 = sites.lastIndexOf("Wiki"); System.out.println("Wiki 最后出現(xiàn)的位置: " + position2); }
總結(jié):
Java中的indexOf()
方法是用于在字符串中查找指定子串的常用方法。主要有以下幾個用法:
int indexOf(String str)
:從頭開始查找指定子串在字符串中第一次出現(xiàn)的位置,返回相應(yīng)的索引值。int indexOf(String str, int startIndex)
:從指定的索引位置開始往后查找,返回第一次出現(xiàn)的位置。int lastIndexOf(String str)
:返回指定子字符串在字符串中最后一次出現(xiàn)的位置。int lastIndexOf(String str, int startIndex)
:從指定的索引位置開始反向查找,返回最后一次出現(xiàn)的位置。
在使用這些方法時,需要注意:
- 如果找到指定子串,返回對應(yīng)的索引位置(索引從0開始)。
- 如果未找到指定子串,返回-1。
此外,指定了索引位置后,從該位置開始進行查找。如果子串為空字符串或搜索索引超出了字符串的長度,將返回相應(yīng)的長度值。
indexOf()
方法在字符串操作中非常實用,可以幫助定位特定字符或子串的位置,為進一步的處理提供了便利。
到此這篇關(guān)于Java中indexOf()方法詳解及其日常使用舉例的文章就介紹到這了,更多相關(guān)Java indexOf()方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合MybatisSQL過濾@Intercepts的實現(xiàn)
這篇文章主要介紹了SpringBoot整合MybatisSQL過濾@Intercepts的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03SpringDataJPA實體類關(guān)系映射配置方式
這篇文章主要介紹了SpringDataJPA實體類關(guān)系映射配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot結(jié)合JSR303對前端數(shù)據(jù)進行校驗的示例代碼
這篇文章主要介紹了SpringBoot結(jié)合JSR303對前端數(shù)據(jù)進行校驗的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09