【发布时间】:2015-03-01 06:48:33
【问题描述】:
我不想要“charAt(1);”返回字符串中位置 1 的字符...我希望它报告“matching = 1;”的所有实例
举个例子
int matching = 1;
int notMatching = 0;
int HI = matching;
int IH = notMatching;
int searchString = theString.charAt(notMatching);
int count = 0;
int index = 0;
while (0 < theString.length())
{
int = theString.charAt(matching);
if (theString.charAt(notMatching) == searchString)
{
count = count + 1;
}
index = index + 1;
}
不是一个很好的例子,但基本上是我想要的,或者这个程序应该做的是接受用户输入:
HIHIIH
并报告 HI 拼写为 IH 的实例......所以返回将是例如;
System.out.println("HI 拼写为 IH" + count + "times");
编辑:“重复”对我没有帮助
【问题讨论】:
-
那么你想统计一个子串出现的次数?也就是你想知道字符串中出现“IH”的次数?
-
有一个
indexOf(String)方法。这就是你想要使用的 -
你认为你可以写一个例子吗?
-
@Donnie 看看我的回答。它解决了你的问题。顺便说一句,您编写的代码根本没有意义..
-
我从您的 sn-p 的编译器中收到“找不到符号”错误。
标签: java string algorithm int charat