day08--String类的练习
This commit is contained in:
@@ -1,8 +1,29 @@
|
||||
package com.inmind.string01;
|
||||
/*
|
||||
常用类_String的练习_拼接字符串
|
||||
定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。格式参照如下:[值1#值2#值3]。
|
||||
定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。
|
||||
格式参照如下:[值1#值2#值3]。
|
||||
|
||||
*/
|
||||
public class Test08 {
|
||||
public static void main(String[] args) {
|
||||
String str = "a,b,c";
|
||||
String result = toString(str.split(","));
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
public static String toString(String[] arr){
|
||||
//[值1#值2#值3]
|
||||
String result = "[";
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
String temp = arr[i];
|
||||
if (i == arr.length - 1) {
|
||||
result = result.concat(temp + "]");
|
||||
} else {
|
||||
result = result.concat(temp + "#");
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,47 @@
|
||||
package com.inmind.string01;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
8.常用类_String的练习_统计字符个数
|
||||
键盘录入一个字符串,统计字符串中大小写字母及数字字符个数
|
||||
|
||||
分析:
|
||||
1.scanner的使用
|
||||
2.接收一个字符串
|
||||
3.统计字符串中大小写字母及数字字符个数(获取字符串的每个字符来判断)
|
||||
*/
|
||||
public class Test09 {
|
||||
public static void main(String[] args) {
|
||||
//创建一个Scanner对象
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("请输入一串字符串:");
|
||||
String str = sc.nextLine();//获取用户输入的一行字符串
|
||||
int bigNum = 0;//大写字母的个数
|
||||
int smallNum = 0;//小写字母的个数
|
||||
int num = 0;//数字字符母的个数
|
||||
|
||||
//字符串转为字符数组
|
||||
char[] chars = str.toCharArray();
|
||||
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
char c = chars[i];
|
||||
if (c >= 'A' && c <= 'Z') {
|
||||
bigNum++;
|
||||
}
|
||||
|
||||
if (c >= 'a' && c <= 'z') {
|
||||
smallNum++;
|
||||
}
|
||||
if (c >= '0' && c <= '9') {
|
||||
num++;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("大写的字母:" + bigNum);
|
||||
System.out.println("小写的字母:" + smallNum);
|
||||
System.out.println("数字:" + num);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user