- // 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
分析:前提:字符串要存在
A:定义三个统计变量 bigCount=0 smallCount=0
numberCount=0
B:遍历字符串,得到每一个字符。 length()和charAt()结合
C:判断该字符到底是属于那种类型的
大:bigCount++ 小:smallCount++ 数字:numberCount++
public class LX2 {
public static void main(String[] args) {
String s=“Hello123World”;
int bigCount=0;
int smallCount=0;
int numberCount=0;
for(int x=0;x<s.length();x++) {
char ch=s.charAt(x);
if(ch>=‘a’&&ch<=‘z’) {
smallCount++;
}else if(ch>=‘A’&&ch<=‘Z’) {
bigCount++;
}else {
numberCount++;
}
}
System.out.println(“小写字母有”+smallCount+“个”);
System.out.println(“大写字母有”+bigCount+“个”);
System.out.println(“数字有”+numberCount+“个”);
}
}
方法二:
import
java.util.Scanner;
public class ArrayDemo {
public static void main(String[] args) {
int bigCount=0;
int smallCount=0;
int numberCount=0;
Scanner sc=new Scanner(System.in);
System.out.println(“请输入一个串:”);
String line=sc.nextLine();
char [] chs=line.toCharArray();
for(int x=0;x<chs.length;x++) {
char ch=chs[x];
if(Character.isUpperCase(ch)) {
bigCount++;
}else if(Character.isLowerCase(ch)) {
smallCount++;
}else if(Character.isDigit(ch)) {
numberCount++;
}
}
System.out.println(“大写字母:”+bigCount+“个”);
System.out.println(“小写字母:”+smallCount+“个”);
System.out.println(“数字:”+numberCount+“个”);
}
}
- // 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
分析:A:先获取第一个字符 B:获取除了第一个字符以外的字符
C:把A转成大写 D:把B转成小写 E:C拼接D
public class LX2 {
public static void main(String[] args) {
String s=“helloWORLD”;
String s1=s.substring(0,1);
String s2=s.substring(1);
String s3=s1.toUpperCase();
String s4=s2.toLowerCase();
String s5=s3.concat(s4);
System.out.println(s5);
}
}
- // 统计大串中小串出现的次数
分析:前提:是已经知道了大串和小串。
A:定义一个统计变量,初始化值是0
B:先在大串中查找一次小串第一次出现的位置
a:索引是-1,说明不存在了,就返回统计变量
b:索引不是-1,说明存在,统计变量++
C:把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串 D:回到B
public class LX3 {
public static void main(String[] args) {
String s1=“woaijavawozhenaijava”;
String s2=“java”;
int count =getCount(s1,s2);
System.out.println(“Java在大串中出现了:”+count+“次”);
}
public static int getCount(String s1,String s2) {
int count=0;
int index;
while((index=s1.indexOf(s2))!=-1) {
count++;
s1=s1.substring(index+s2.length());
}
return count;
}
}
- // 需求:把数组中的数据按照指定个格式拼接成一个字符串
分析:A:定义一个字符串对象,只不过内容为空
B:先把字符串拼接一个"[" C:遍历int数组,得到每一个元素
D:先判断该元素是否为最后一个
是:就直接拼接元素和"]" 不是:就拼接元素和逗号以及空格
E:输出拼接后的字符串
public class lx1 {
public static void main(String[] args) {
int [] arr= {1,2,3};
String s="";
s+="[";
for(int x=0;x<arr.length;x++) {
if(x==arr.length-1) {
s+=arr[x];
s+="]";
}else {
s+=arr[x];
s+=", ";
}
}
System.out.println(“最终的字符串是:”+s);
}
}
- // 字符串反转
分析:A:键盘录入一个字符串 B:定义一个新字符串
C:倒着遍历字符串,得到每一个字符
a:length()和charAt()结合 b:把字符串转成字符数组
D:用新字符串把每一个字符拼接起来 E:输出新串
import
java.util.Scanner;
public class lx2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println(“请输入一个字符串:”);
String line=sc.nextLine();
String s=myReverse(line);
System.out.println(“实现功能后的结果是:”+s);
}
public static String
myReverse(String s) {
String result="";
char[] chs=s.toCharArray();
for(int x=chs.length-1;x>=0;x–) {
result+=chs[x];
}
return result;
}
}