思路:
1.使用split()方法分割字符串,得到被分割后的字符串数组。
2.遍历字符串数组,作比较得到个数。
步骤:
1.创建目标字符串
String s1 = “i am a student,i am ,i,am”;
2.替换掉逗号
s1 = s1.replaceAll(",", " “);
3.以空格分割字符串得到字符串数组
String[] split = s1.split(” ");
4遍历数组
代码:
public static void main(String[] args) {
int tem = 0;
String s1 = "i am a student,i am ,i,am";
s1 = s1.replaceAll(",", " ");
System.out.println(s1.toString());
String[] split = s1.split(" ");
for(String s:split) {
if(s.equals("am"))//此处查询的是am单词
tem++;
}
System.out.println(tem);
}
本文介绍了一种通过Java实现的字符串处理方法,包括如何去除特定字符、按指定符号分割字符串并统计子串出现次数的过程。具体步骤涉及字符串的替换、分割及遍历比较等操作。

863

被折叠的 条评论
为什么被折叠?



