gpt4 book ai didi

java - 获取特定字符串后的下一个字符/字符串

转载 作者:行者123 更新时间:2023-12-01 17:59:19 25 4
gpt4 key购买 nike

我想在给定的字符串段落中获取下一个字符,因为只要重复该单词,就应该打印字符串的下一个字符,假设最多还有 6 个字符来获取给定单词后所需的字符串。

例如。摇滚

我尝试过这个,但没有得到所需的输出。

String example = "Hi man check rock,let's go on together to the sight of rock now Print upto 6 char";
System.out.println(example.substring(example.indexOf("rock") + 5));

最佳答案

您可以选择带有子字符串的开始和结束。试试这个:

int stringindex = example.indexOf("rock");   //Save the index to a variable so you don't have to call the indexOf method twice. 
int stringlength = "rock".length();
example.substring(stringindex + stringlength, stringindex + stringlength + 5); //Select next 5 characters from the index of rock.

关于java - 获取特定字符串后的下一个字符/字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42268417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com