【发布时间】:2016-02-23 13:04:55
【问题描述】:
如何检查字符串是否包含用户喜欢的给定单词的字母
Enter the pattern string: shl
Enter a string: school
shl occurs in ”school”
Enter a string: okul
shl does NOT occur in ”okul”
Enter a string: salaries are neither high nor low
shl occurs in ”salaries are neither high nor low”
Enter a string: This is a good school, isn’t it?
shl occurs in ”This is a good school, isn’t it?”
Enter a string: I love programming
shl does NOT occur in ”I love programming”
if (occursIn(pattern,str)){
System.out.println(pattern + " occurs in " + str);
}
else{
System.out.println(pattern + " does not occurs in " + str);
}
public static boolean occursIn(String pattern, String str){
for(int i=0;pattern.charAt(i)<=str.length();i++){
if(str.contains(pattern.charAt(i))){
return true;
}
else {
return false;
}
}
}
它在方法中不起作用。我尝试了 5 个小时,请帮助我!!
我无法比较所有字母并将它们返回给 main 方法
【问题讨论】:
-
自己尝试,如果无法解决,请发布您的尝试,然后我们可以帮助您。
标签: java string letter contain