首页  >  文章  >  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删除