如何用java求一个字符串在另一个字符串中出现的位置?
输入任意一个字符串!输入在上一个字符串中的一个或一组字或词!计算这个字或词在输入的那个字符串中出现了几次!...
输入任意一个字符串!输入在上一个字符串中的一个或一组字或词!计算这个字或词在输入的那个字符串中出现了几次!
展开
2个回答
展开全部
import java.util.*;
public class CountChars {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please Input Your String!");
String str = sc.nextLine();
Map<Character, Integer> map = countLetters(str);
System.out.println("total kinds: " + map.size());
for (Map.Entry<Character, Integer> entry : map.entrySet()) { //增强的for循环
System.out.printf("letter %c: %d\n", entry.getKey(), entry.getValue());
}
}
static Map<Character, Integer> countLetters(String s) {
if (s == null) {
return null;
}
Map<Character, Integer> map = new HashMap<Character, Integer>();
char c;
Integer oldValue;
int newValue;
for (int i = 0; i < s.length(); ++i) {
c = s.charAt(i);
oldValue = map.get(c);
newValue = (oldValue == null) ? 1 : oldValue.intValue() + 1;
map.put(c, newValue);
}
return map;
}
}
public class CountChars {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please Input Your String!");
String str = sc.nextLine();
Map<Character, Integer> map = countLetters(str);
System.out.println("total kinds: " + map.size());
for (Map.Entry<Character, Integer> entry : map.entrySet()) { //增强的for循环
System.out.printf("letter %c: %d\n", entry.getKey(), entry.getValue());
}
}
static Map<Character, Integer> countLetters(String s) {
if (s == null) {
return null;
}
Map<Character, Integer> map = new HashMap<Character, Integer>();
char c;
Integer oldValue;
int newValue;
for (int i = 0; i < s.length(); ++i) {
c = s.charAt(i);
oldValue = map.get(c);
newValue = (oldValue == null) ? 1 : oldValue.intValue() + 1;
map.put(c, newValue);
}
return map;
}
}
展开全部
楼上的方法很好,我来写个我的一个方法
import java.util.*;
public class Count
{
static int countNum=0;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please Input Your String!");
String str = sc.nextLine();
System.out.println("Please Input Your subString!");
String substr = sc.nextLine();
count(str, substr);
System.out.println(substr+" exist in "+str+" "+countNum+" times");
}
public static void count(String str, String substr)
{
int a = str.indexOf(substr);
if (a != -1)
{
str = str.substring(a+substr.length());
countNum++;
count(str, substr);
}
}
}
import java.util.*;
public class Count
{
static int countNum=0;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please Input Your String!");
String str = sc.nextLine();
System.out.println("Please Input Your subString!");
String substr = sc.nextLine();
count(str, substr);
System.out.println(substr+" exist in "+str+" "+countNum+" times");
}
public static void count(String str, String substr)
{
int a = str.indexOf(substr);
if (a != -1)
{
str = str.substring(a+substr.length());
countNum++;
count(str, substr);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询