判断字符串中字符出现的次数+去除空格


//判断字符串中字符出现的次数
1
package 字符串; 2 3 public class 字符串相关 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 8 String s = "mingrikejijavabu"; 9 System.out.println("输出字符串:"+" "+s); 10 String s2 = s.replace("i", ""); 11 System.out.println("替换所有的i:"+" "+s2); 12 System.out.println("可判断字符串中i出现的次数:"+(s.length()-s2.length())); 13 14 } 15 16 }

这个问题还有一个想法:用读取字符串的索引位置,来判断次数。

但是没写出来。。。

2、

//去除字符串中的空格 split 分割
 1 package 字符串;
 2 
 3 public class 字符串相关 {
 4 
 5     public static void main(String[] args) {
 6         // TODO 自动生成的方法存根
 7 
 8         String s0 = " I Love U you ";
 9         String[] s1 = s0.split(" ");
10         System.out.println("输出分割后的字符串:"+s0);
11         System.out.print("输出分割后的字符串:");
12         for(int i=0; i<s1.length; i++)
13         {
14             System.out.print(s1[i]);
15         }
16 
17     }
18 
19 }


 
原文地址:https://www.cnblogs.com/hellodp/p/5128773.html