首頁  >  文章  >  Java  >  在字串中尋找特定單字的最後一個索引的Java程序

在字串中尋找特定單字的最後一個索引的Java程序

WBOY
WBOY轉載
2023-09-17 18:01:021246瀏覽

在字串中尋找特定單字的最後一個索引的Java程序

字串是字元序列。在Java中,字串也是一個物件。它是String類別的物件。字串中特定單字的最後一個索引只不過是該單字在字串中最後一次出現的位置。字串中字元的索引定義或告訴字串中的位置。位置或索引總是從 0 開始。在本節中,我們將討論如何實作一個 java 程式來尋找字串中特定單字的最後一個索引。字串在 Java 中是不可變的,即;字串不能被操作。如果我們操作一個字串,就會建立一個新物件。

範例

輸入

searchString = "He is the one and only one male person in our team" word = "one"

輸出

The last index is 23

解釋 - 在上面的範例中,單字「one」出現在搜尋字串中的索引 10 和索引 23 處。由於 23 是單字「one」的最後一個索引,因此輸出為 23。

輸入

searchString = "Monkey eats banana" word = "eats"

輸出

The last index is 7

解釋 - 在上面的範例中,單字「eats」出現在搜尋字串的索引 7 處。由於 7 是單字“eats”的最後一個索引,因此輸出為 7。

現在,我們將討論 Java 中查找字串中特定單字的最後一個索引的各種方法。

方法一:使用lastIndexOf()方法

在這種方法中,我們將使用 java 提供的 lastIndexOf() 內建方法。 String 類別並尋找字串中特定單字的最後一個索引。

文法

下面的語法是指該方法的用法

strobj.lastIndexOf(string)

此方法傳回作為輸入參數給出的特定字串的最後一個索引

演算法

  • 宣告並初始化一個字串。

  • 初始化我們需要尋找其最後一個索引的字串。

  • 使用lastIndexOf()方法來尋找給定字串中給定單字的最後一個索引並將其儲存在變數中。

  • 列印變數值以取得最後一個索引值。

範例

在此範例中,我們初始化要搜尋的字串和單字,並使用「lastIndexOf()」方法。如果輸入參數字串不存在,則傳回 -1;如果存在,則傳回該字串最後出現的索引。

// Java program to find lastIndex of a particular word in string.
import java.util.*;
public class Main {
   public static void main(String[] args) {
      String str = "He is the one and only one male person in our team";
      String word = "one";
      int lastindex = str.lastIndexOf(word);
      if (lastindex == -1) {
         System.out.println("The word not present in string.");
      } else {
         System.out.println("The last index of the searched word ==> " + word + " is "+ lastindex);
      }
   }
}

輸出

The last index of the searched word ==> one is 23

方法2:使用indexOf()方法

在這個方法中,我們將使用 java.String 類別提供的 indexOf() 內建方法並尋找字串中特定單字的最後一個索引。

演算法

  • 宣告並初始化一個字串。

  • 初始化我們需要找出其最後一個索引的單字。

  • 使用indexOf()方法找出單字在字串中的索引,該方法傳回該單字的第一次出現並將其指派給索引變數

  • #使用while 循環,將索引分配給最後一個索引,並為每個索引值在字串中已找到的索引位置之後找到單字的新索引,使用indexOf() 方法並重複查找直到索引值是-1。

  • 列印最後一個索引,否則列印-1。

範例

在下面的範例中,我們用字串初始化兩個變量,並使用‘indexOf()’方法來尋找字串中搜尋單字的起始索引。找到第一個索引後,使用 while 迴圈重複此操作,但每次找到新的索引位置時,都需要使用找到的新索引位置來更新「indexOf()」方法中的 start_index 參數位置。因此,最後索引值變為-1,並且在每個循環中我們將前一個索引值儲存在lastIndex中,最後列印lastIndex值。

//java program to find last index of a particular word in a string using indexOf() method
import java.util.*;
public class Main {
   public static void main(String[] args) {
      String str = "He is the one and only one male person in our team";
      String word = "one";
      int lastindex = -1;
      int index = str.indexOf(word);
      while (index != -1) {
         lastindex = index;
         index = str.indexOf(word, index + 1);
      }
      if (lastindex == -1) {
         System.out.println("The word not present in string.");
      } else {
         System.out.println("The last index  is " + lastindex);
      }
   }
}

輸出

The last index  is 23

方法-3:使用regionMatches()方法

在這個方法中,我們將使用 java.String 類別提供的 RegionMatches() 內建方法並尋找字串中特定單字的最後一個索引。

文法

public boolean regionMatches(boolean ignoreCase, int toffset,  String other,  int offset,  int len)

參數

  • int offset - 要比較的第一個字串區域的起始索引。

  • String other - 要比較的字串。

  • int offset - 要比較的另一個字串區域的起始索引。

  • int len - 要比較的字元數。

  • booleanignoreCase - 告訴是否不區分大小寫進行比較。如果此參數值為“true”,則比較不區分大小寫,否則反之亦然。

如果兩個字串的指定區域匹配,此方法傳回 true,否則傳回 false。

下面的範例參考了該方法的用法 -

String str1 = "Hello, world!";
String str2 = "HELLO, WORLD!";
boolean match1 = str1.regionMatches(0, str2, 0, 12, false); // match1 is false
boolean match2 = str1.regionMatches(true, 0, str2, 0, 12); // match2 is true

演算法

  • 用字串初始化變數 str

  • 用要搜尋的字串初始化另一個變數word,索引為-1。

  • 使用 for 循環,使用「regionMatches()」方法尋找索引並列印值。

示例

在此示例中,初始化了两个带有字符串的变量,并初始化了一个带有 -1 的 lastIndex 变量。然后我们从字符串的最后一个开始迭代,每次迭代时,我们使用“regionMatches()”方法检查字符串区域是否与搜索字符串匹配,如果匹配,则该方法返回 true,因此我们可以存储 lastIndex 值并打印值。

//Java program to find the last index of a particular word in a string
import java.util.*;
public class Main {
   public static void main(String[] args) {
      String str = "Monkey eats banana";
      String word = "eats";   
      int index = -1;
      for(int i = str.length() - word.length(); i >= 0; i--) {
         if(str.regionMatches(i, word, 0, word.length())) {
            index = i;
            break;
         }
      }
      System.out.println("Last index  is " + index);
   }
} 

输出

Last index  is 7

因此,我们在本文中讨论了查找字符串中特定单词的最后位置的不同方法。

以上是在字串中尋找特定單字的最後一個索引的Java程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除