我有字符串
a.b.c.d
Run Code Online (Sandbox Code Playgroud)
我想计算'.'的出现次数.以惯用的方式,最好是单线.
(以前我把这个约束表达为"没有循环",如果你想知道为什么每个人都试图回答而不使用循环).
有没有一种简单的方法(而不是手动遍历所有的字符串,或循环遍历indexOf),以便找到一个字符出现在字符串中的次数?
假设我们有"abdsd3 $ asda $ asasdd $ sadas",我们希望$出现3次.
我是Java Strings的新手,问题是我想要计算String中特定单词的出现次数.假设我的String是:
i have a male cat. the color of male cat is Black
Run Code Online (Sandbox Code Playgroud)
现在我也不想将它拆分,所以我想搜索一个"雄猫"这个词.它在我的字符串中出现两次!
我在想的是:
int c = 0;
for (int j = 0; j < text.length(); j++) {
if (text.contains("male cat")) {
c += 1;
}
}
System.out.println("counter=" + c);
Run Code Online (Sandbox Code Playgroud)
它给了我46计数值!那么解决方案是什么?
我试图在Java中查找字符串中所有出现的子字符串.
例如:搜索"ababsdfasdfhelloasdf"代表"asdf"将返回[8,17],因为有2个"asdf",一个位于8位,一个位于17位.搜索"aaaaaa"中的"aa"将返回[0,1, 1,2,3,4]因为在位置0,1,2,3和4处有"aa".
我试过这个:
public List<Integer> findSubstrings(String inwords, String inword) {
String copyOfWords = inwords;
List<Integer> indicesOfWord = new ArrayList<Integer>();
int currentStartIndex = niwords.indexOf(inword);
int indexat = 0;
System.out.println(currentStartIndex);
while (cthing1 > 0) {
indicesOfWord.add(currentStartIndex+indexat);
System.out.println(currentStartIndex);
System.out.println(indicesOfWord);
indexat += cthing1;
copyOfWords = copyOfWords.substring(cthing1);
System.out.println(copyOfWords);
cthing1 = copyOfWords.indexOf(inword);
}
Run Code Online (Sandbox Code Playgroud)
这个问题可以在Python中解决如下:
indices = [m.start() for m in re.finditer(word, a.lower())]
Run Code Online (Sandbox Code Playgroud)
"word"是我正在寻找的单词,"a"是我正在搜索的字符串.
我怎样才能在Java中实现这一目标?
可能重复:
字符串中子字符串的出现
如在主题中如何检查一个字符串包含另一个字符串的次数?例:
s1 "babab"
s2 "bab"
Result : 2
Run Code Online (Sandbox Code Playgroud)
如果我使用Matcher它只识别第一次出现:
String s1 = JOptionPane.showInputDialog(" ");
String s2 = JOptionPane.showInputDialog(" ");
Pattern p = Pattern.compile(s2);
Matcher m = p.matcher(s1);
int counter = 0;
while(m.find()){
System.out.println(m.group());
counter++;
}
System.out.println(counter);
Run Code Online (Sandbox Code Playgroud)
我可以这样做,但我想在下面使用Java库iike Scanner,StringTokenizer,Matcher等:
String s1 = JOptionPane.showInputDialog(" ");
String s2 = JOptionPane.showInputDialog(" ");
String pom;
int count = 0;
for(int i = 0 ; i< s1.length() ; i++){
if(s1.charAt(i) == s2.charAt(0)){
if(i + s2.length() <= s1.length()){
pom = s1.substring(i,i+s2.length());
if(pom.equals(s2)){
count++;
} …Run Code Online (Sandbox Code Playgroud) 我正在使用StringUtils.abbreviateMiddle(string, middle, targetLength);缩写中间的字符串,但使用此方法切断了单词.有没有一种方法,它只会在最接近弦的中间位置的空间切断弦?
public static String shortenStringMiddle(String string, String middle, int targetLength) {
targetLength = Math.abs(targetLength);
if (string != null && string.length() > targetLength) {
return StringUtils.abbreviateMiddle(string, middle, targetLength);
}
else {
return string;
}
}
Run Code Online (Sandbox Code Playgroud)
输入如果应用于此文本:
东南亚鱼虾养殖的疾病控制 - 诊断和畜牧技术:SEAFDEC-OIE研讨会论文集 - 东南亚鱼虾养殖疾病控制研讨会 - 诊断和畜牧技术,2001年12月4日至6日,伊洛伊洛市,菲律宾
System.out.println(StringUtils.abbreviateMiddle("Disease Control in Fish and Shrimp Aquaculture in Southeast Asia - Diagnosis and Husbandry Techniques: Proceedings of the SEAFDEC-OIE Seminar-Workshop on Disease Control in Fish and Shrimp Aquaculture in Southeast Asia - Diagnosis and …Run Code Online (Sandbox Code Playgroud) 所以,我正在研究一个程序,我需要检查一个字符串是否有多个点.我去看了一下文档regex,发现了这个:
X{n,m}? X, at least n but not more than m times
Run Code Online (Sandbox Code Playgroud)
按照这个逻辑,我尝试了这个:
mString = "He9.lo,"
if (myString.matches(".{1,2}?")){
System.out.print("it works");
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用(它没有给出任何错误,它只是不会做内部的事情if).我认为问题是认为点意味着别的东西.我也看了这个问题:正则表达式,但是对于PHP,我不知道如何将其应用于java.
编辑:基本上我想知道一个字符串是否包含多个点,例如:
他...... lo(True,包含多个),hel.llo(False,包含一个),.. shell.o(True包含多个).
任何帮助,将不胜感激.
可能重复:
字符串函数如何计算字符串行中的分隔符
我有一个字符串as str ="one $ two $ three $ four!five @ six $"现在如何使用java代码计算该字符串中"$"的总数.
返回字符串"hello/Hello/... etc"出现在给定字符串中的任何位置的次数.
问题的不同之处在于
字符串hello可以是任何情况,即大写或小写.
样本输入#1
计数("abc hello def")
样本输出#1
1
样本输入#2
计数("你好.你好.好.")
样本输出#2
2
样本输入#3
计数("hi")
样本输出#3
0
MyApproach
public int count(String str)
{
String str1="Hello";
int l=str.length();
int l1=str1.length();
if(l<l1)
{
return 0;
}
else
{
int count=0;
int p=0;
int j=0;
while(j<l)
{
char c=str.charAt(j);
char c1=str1.charAt(p);
if(c==c1)
{
p++;
if(p==l1)
{
count++;
p=0;
}
}
else
{
p=0;
}
j++;
}
return count;
}
}
Output TestcaseParameters Testcase Actual Answer Expected
No output 'HELLO how are you' …Run Code Online (Sandbox Code Playgroud)