Java 程序查找字符串中特定单词的最后一个索引

javaobject oriented programmingprogramming更新于 2024/8/5 22:55:00

字符串是字符序列。在 Java 中,字符串也是一个对象。它是 String 类 的对象。字符串中特定单词的最后一个索引只不过是该单词在字符串中最后一次出现的位置。字符串中字符的索引定义或告知字符串中的位置。位置或索引始终从 0 开始。在本节中,我们将讨论如何实现 Java 程序来查找字符串中特定单词的最后一个索引。字符串在 Java 中是不可变的,即字符串无法被操纵。如果我们操作一个字符串,就会创建一个新对象。

示例

输入

searchString = "他是我们团队中唯一的男性" word = "one"

输出

最后一个索引是 23

说明 - 在上面的例子中,单词"one"出现在 searchString 中的索引 10 和索引 23 处。因为 23 是单词"one"的最后一个索引,所以输出为 23。

输入

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

输出

最后一个索引为 7

说明 - 在上面的例子中,单词"eats"出现在 searchString 的索引 7 处。因为 7 是单词"eats"的最后一个索引,所以输出为 7。

现在,我们将讨论在 Java 中查找字符串中特定单词的最后一个索引的各种方法。

方法 1:使用 lastIndexOf() 方法

在这种方法中,我们将使用 Java 提供的 lastIndexOf() 内置方法。 String 类并查找字符串中特定单词的最后一个索引。

语法

以下语法指的是该方法的用法

strobj.lastIndexOf(string)

此方法返回作为输入参数给出的特定字符串的最后一个索引

算法

  • 声明并初始化一个字符串。

  • 初始化我们需要查找其最后一个索引的字符串。

  • 使用 lastIndexOf() 方法在给定字符串中查找给定单词的最后一个索引并将其存储在变量中。

  • 打印变量值以获取最后一个索引值。

示例

在此示例中,我们初始化要搜索的字符串和单词,并使用"lastIndexOf()"方法。如果输入参数字符串不存在,则返回 -1;如果存在,则返回该字符串出现的最后一个索引。

// Java 程序用于查找字符串中特定单词的 lastIndex。
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 程序使用 indexOf() 方法查找字符串中特定单词的最后一个索引
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 − 要比较的字符数。

  • boolean ignoreCase − 指示是否不区分大小写进行比较。如果此参数值为"true",则比较不区分大小写,反之亦然。

如果两个字符串的指定区域匹配,则此方法返回 true,否则返回 false。

以下示例参考了该方法的用法 −

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

算法

  • 使用字符串初始化变量 str

  • 使用要搜索的字符串初始化另一个变量 word,并将索引设置为 -1。

  • 使用 for 循环,使用"regionMatches()"方法查找索引并打印值。

示例

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

//Java 程序用于查找字符串中特定单词的最后一个索引
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

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


相关文章