gpt4 book ai didi

java - 一个单词在一个TXT文件中出现了多少次

转载 作者:行者123 更新时间:2023-11-29 06:41:37 24 4
gpt4 key购买 nike

当我尝试计算一个单词在 TXT 文件中出现的次数时遇到问题。

  1. 我创建了一个文本字段 ( txta )
  2. 我创建了一个按钮来应用操作 (btn)
  3. 我创建了一个显示文件内容的文本区域(区域)

当我选择文件时,文件的内容显示在区域中。然后我在txta中输入单词进行搜索。然后我点击了 btn,但代码不起作用。

public int contarPalabras(String chain, String word) {
// English translation:
// Receive a string and a word and return the amount of times
// that the word was found in the string.
// If the letter is not found, return (-1).

int cant = 0;

int intIndex = chain.indexOf(word);

if (intIndex == -1) {
cant = -1;
} else {
cant = intIndex;
}

return cant;
}

最佳答案

commons-lang 有 StringUtils.countMatches(str, sub)这正是你想要的。

关于java - 一个单词在一个TXT文件中出现了多少次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11277692/

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