gpt4 book ai didi

java - 获取字符串中特定单词之后的字符?

转载 作者:行者123 更新时间:2023-12-01 14:43:48 25 4
gpt4 key购买 nike

我有一串文本和一个需要在该字符串中查找的特定单词。一旦我找到字符串中该单词的下一个出现,我需要以某种方式找到直接跟在该单词后面的字符。我尝试使用扫描仪,但它不包含 nextChar() 方法,并且此类方法可以达到我想要做的目的。我还能尝试什么?这是我的代码,但它没有执行我想要的操作。

public void storeChar(String str)  //used to get the char after the seed and store it in the arraylist of chars
{
Scanner sc = new Scanner(str);
seed = wrdSd.getSeed();
while(sc.hasNext()) //while there are words left to read
{
String next = sc.next();

if(next.equals(seed)) //if the next word of the scanner = the seed
{
ch = sc.next().charAt(0); //gets char at index 0 of the next word
singleChar.add(ch); //adds the char to the char arraylist
}
}
sc.close();

}

提前致谢!

最佳答案

使用String#indexOf判断单词是否在字符串中的方法。如果是,则使用返回的索引,添加单词的长度,然后使用 charAt 方法将该结果用作字符串的索引。请小心,不要超出字符串的末尾。

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

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