search
HomeJavaFind specific word index after given word in String Java

In Java programming, finding the index of a specific word after a given word is a common problem. In the String class, there are multiple methods to achieve this functionality. This article will demonstrate through specific cases how to use these methods in Java to find the target word and obtain its index position. Let us follow the guidance of PHP editor Apple and learn more about this useful technique.

Question content

Suppose I have a string

String str = "Hello all, the world is going to end here and I am the only end character which is in the end game of the end world";

Now I want to write a function in which I pass the entire string, matchingword, wordtofindaftermatchingword

So suppose I want to find the index of "game", but the "game" word should come after the "end" word, and the "end" word can appear n times in the string. I don't want to use regular expressions because I'm thinking of making this function generic.

Solution

As someone said, you can use the method indexof(string,int) with a starting index:

public static int indexof(string str, string word, string precededby) {
    int i = str.indexof(precededby);
    if (i < 0) {
        return -1;
    }
    return str.indexof(word, i+precededby.length());
}

Now this:

System.out.println(indexOf(str, "game", "end"));

will print 94

The above is the detailed content of Find specific word index after given word in String Java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete
Java如何使用String类的join()函数将多个字符串拼接为一个字符串Java如何使用String类的join()函数将多个字符串拼接为一个字符串Jul 26, 2023 pm 03:37 PM

Java如何使用String类的join()函数将多个字符串拼接为一个字符串在Java中,String类是一个常用的类,用来表示字符串。它提供了许多用于操作字符串的方法,其中一个重要的方法就是join()函数。该函数可以将多个字符串拼接为一个字符串,并且可以指定一个分隔符来分隔每个字符串。本文将介绍如何使用join()函数来实现字符串的拼接操作。使用Stri

Java文档解读:String类的length()方法详解Java文档解读:String类的length()方法详解Nov 03, 2023 pm 12:24 PM

Java文档解读:String类的length()方法详解String类是Java语言中最常用的类之一,它提供了一系列对字符串进行操作的方法。其中,length()方法是String类中的一个常用方法之一。本文将对String类的length()方法进行详细的解读,并提供具体的代码示例。一、length()方法的定义在Java文档中,String类的leng

Java如何使用String类的getBytes()函数将字符串转换为字节数组Java如何使用String类的getBytes()函数将字符串转换为字节数组Jul 25, 2023 pm 08:09 PM

Java如何使用String类的getBytes()函数将字符串转换为字节数组在Java中,String类是以字符形式存储字符串的,而有时候我们需要将字符串转换成字节数组进行处理,这时就可以使用String类的getBytes()函数来完成转换。getByte()函数会将字符串编码成指定的字节数组,并返回该字节数组。下面我将介绍如何

Java如何使用String类的indexOf()函数查找字符串中的指定字符或子串Java如何使用String类的indexOf()函数查找字符串中的指定字符或子串Jul 24, 2023 pm 06:13 PM

Java如何使用String类的indexOf()函数查找字符串中的指定字符或子串引言:在Java中,String类是很常用的类之一,它提供了很多方法来操作字符串。其中indexOf()函数是用于查找字符串中指定字符或子串的方法之一。本文将详细介绍Java中如何使用String类的indexOf()函数来实现字符串的查找操作,并提供一些示例代码以帮助读者更好

Java如何使用String类的toUpperCase()函数将字符串转换为大写Java如何使用String类的toUpperCase()函数将字符串转换为大写Jul 26, 2023 pm 04:01 PM

Java如何使用String类的toUpperCase()函数将字符串转换为大写在Java中,String类是一个非常常用的类,该类提供了许多用于处理字符串的方法。其中一个非常有用的方法是toUpperCase(),它能够将一个字符串转换为大写形式。toUpperCase()方法的使用非常简单,只需要调用该方法即可。以下是一段示例代码,展示了如何使用toUp

Java如何使用String类的toLowerCase()函数将字符串转换为小写Java如何使用String类的toLowerCase()函数将字符串转换为小写Jul 24, 2023 pm 07:51 PM

Java如何使用String类的toLowerCase()函数将字符串转换为小写在Java编程中,有时候需要将字符串转换为小写形式,以便进行字符串比较或其他相关操作。幸运的是,Java的String类提供了一个非常方便的函数toLowerCase()来实现这一目的。本文将简要介绍该函数的用法,并提供一些代码示例来帮助读者更好地理解。首先,让我们来了解一下to

Java变量的初始化与赋值方法探究Java变量的初始化与赋值方法探究Feb 22, 2024 pm 12:12 PM

Java变量的初始化与赋值方法探究在Java编程中,变量的初始化和赋值是非常重要的概念。它们决定了变量在使用前的状态和取值,直接影响到程序的正确性和运行结果。本文将探究Java中变量的初始化和赋值方法,并通过具体的代码示例来说明。一、变量的初始化变量的初始化是指在声明变量的同时给它一个初始值。在Java中,对于不同类型的变量,有不同的默认初始值规则:基本类型

Java如何使用String类的format()函数将数据按指定格式转换为字符串Java如何使用String类的format()函数将数据按指定格式转换为字符串Jul 24, 2023 am 11:12 AM

Java如何使用String类的format()函数将数据按指定格式转换为字符串在Java开发中,经常会遇到需要将数据按照指定格式转换为字符串的情况。Java提供了String类的format()函数,它可以根据指定的格式将数据转换为字符串。本文将介绍如何使用format()函数,并通过代码示例来演示该函数的用法。String类的format()函数是一个静

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.