- 1. 统计一个字符串中大写字母,小写字母,数字出现的次数
- 2. 把一个字符串的首字母转成大写,其余的都是小写
- 3. 把一个字符串的首字母转成大写,其余的都是小写
- 4. 字符串翻转
- 4. 统计大串中小串出现的次数
肯定的微笑
准备,往前出发
1. 统计一个字符串中大写字母,小写字母,数字出现的次数
String s = "HadoopJava12138";
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(c >= '0' && c <= '9'){
numberCount++;
}else if(c >= 'a' && c <= 'z'){
smallCount++;
}else if(c >= 'A' && c <= 'Z'){
bigCount++;
}
}
System.out.println("大写个数:"+bigCount);
System.out.println("小写个数:"+smallCount);
System.out.println("数字个数:"+numberCount);
2. 把一个字符串的首字母转成大写,其余的都是小写
String s = "HadoOPJava";
//方式一
String s1 = s.substring(0,1);
String s2 = s.substring(1);
s1 = s1.toUpperCase();
s2 = s2.toLowerCase();
String s3 = s1.concat(s2);
//链式编程
String s4 = s.substring(0,1)
.toUpperCase()
.concat(s.substring(1)
.toLowerCase());
System.out.println(s3);
System.out.println(s4);
//方式二
char c = s.charAt(0);
//字符转大写方法
c = Character.toUpperCase(c);
String a = c + s.substring(1).toLowerCase();
System.out.println(a);
3. 把一个字符串的首字母转成大写,其余的都是小写
把数组中的数据按照指定个格式拼接成一个字符串
int[] arr = {1,2,3};
String s = "[";
for(int i = 0; i < arr.length; i++){
if(i == arr.length-1){
s += arr[i];
}else {
s += arr[i] + ",";
}
}
s +="]";
System.out.println(s);
4. 字符串翻转
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
String s = sc.next();
String s1 ="";
char[] c = s.toCharArray();
for(int i = c.length-1; i >=0; i--){
s1 += c[i];
}
System.out.println(s1);
4. 统计大串中小串出现的次数
String s = "woaija vawozhenaija vawozhendeaija vawozhendehenaija ";
String key = "123";
int count = 0;
while(true){
int position = s.indexOf(key);
if(position == -1){
break;
}
s = s.substring(position + key.length());
count++;
}
System.out.println(“出现次数为:”count);



