如何用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;
}
}
TableDI
2024-07-18 广告
2024-07-18 广告
作为上海悉息信息科技有限公司的一员,我们专注于提供高效的数据处理解决方案。对于多个文件表格的合并需求,我们通常采用专业的数据整合技术,确保数据的准确性和一致性。通过精确匹配表格字段和格式,我们能够快速、准确地将多个表格合并成一个,为用户提供...
点击进入详情页
本回答由TableDI提供
展开全部
楼上的方法很好,我来写个我的一个方法
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);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询