gpt4 book ai didi

java - 如何使用 indexOf 和 substring 方法对字符串进行标记

转载 作者:行者123 更新时间:2023-12-02 05:47:49 25 4
gpt4 key购买 nike

所以我必须对一个字符串进行分词,而且我只能使用这2种方法来分词我有基础,但不知道要放什么我的 friend 做到了,但我忘了它是什么样的,它是这样的我记得他用制表符的长度来分割它

public class Tester
{
private static StringBuffer sb = new StringBuffer ("The cat in the hat");
public static void main(String[] args)
{
for(int i = 0; i < sb.length() ; i++)
{
int tempIndex = sb.indexOf(" ", 0);
sb.substring(0,tempIndex);
if(tempIndex > 0)
{
System.out.println(sb.substring(0,tempIndex));
sb.delete(0, sb.length());
}

}

}
}

最佳答案

String.indexOf(int ch)返回字符的索引。如果你这样做sb.indexOf(' ')您将获得空间的第一个索引。您可以将其与 substring() 结合使用:sb.substring(0,sb.indexOf(' ')-1)将为您带来第一个 token 。

这似乎是一个家庭作业问题,所以我不想给你完整的答案,但你可能可以解决。如果您需要更多帮助,请发表评论。

关于java - 如何使用 indexOf 和 substring 方法对字符串进行标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23879656/

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