千锋“逆战”学习第22天
每日一句:IF(BOOL 学习= =FALSE)BOOL 落后=TRUE;不断的学习,我们才能不断的前进。
今天学习了StringBuilder和String类方法。
明天继续加油。
作业
7.从命令行上输入一个字符串,用两种不同的方法,把该字符串转换为int类型
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入一个字符串:");
String str = input.nextLine();
// 1.String直接转换为int
int i1 = Integer.parseInt(str);
System.out.println("1.String直接转换为int");
System.out.println(i1);
// 2.String先转化为Integer,再转换为int
Integer i2 = new Integer(str);
int i3 = i2.intValue();
System.out.println("2.String先转化为Integer,再转换为int");
System.out.println(i3);
}
}
运行结果:
输入一个字符串:
123456
1.String直接转换为int
123456
2.String先转化为Integer,再转换为int
123456
--------------------------------------------------------------------------
8.有如下代码:
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return name + " " + age;
}
}
public class TestStudent {
public static void main(String[] args) {
Student stu1 = new Student("tom", 18);
System.out.println(/*1*/);
}
}
在/*1*/位置,填入下列什么代码编译通过
A.stu1 + ” ” + 100
B.100 + ” ” + stu1
C.” ” + 100 + stu1
D.stu1 + 100 + ” ”
答案:A B C ,D错在stu1+100为对象+整型,无意义
--------------------------------------------------------------------------
11.获取邮箱“[email protected]”中的用户名“zhengcg”
public class Test {
public static void main(String[] args) {
String email = "[email protected]";
int elementA = email.indexOf("@");
int elementB = email.indexOf(".");
if(elementA!=-1&&elementB!=-1) {
if(elementA<elementB) {
System.out.println(email+" 邮箱名合法");
}
}
}
}
运行结果:
用户名:zhengc
--------------------------------------------------------------------------
12.验证邮箱“[email protected]”是否合法
public class Test {
public static void main(String[] args) {
String email = "[email protected]";
int elementA = email.indexOf("@");
int elementB = email.indexOf(".");
if(elementA!=-1&&elementB!=-1) {
if(elementA<elementB) {
System.out.println(email+" 邮箱名合法");
}
}
}
}
运行结果:
zhengcg@zparkhr.com 邮箱名合法
--------------------------------------------------------------------------
13.将随机获得的UUID的“-”去掉
public class Test {
public static void main(String[] args) {
String uuid = UUID.randomUUID().toString();
System.out.println(uuid);
System.out.println("去掉“-”后:");
String newUuid = uuid.replaceAll("-", "");
System.out.println(newUuid);
}
}
运行结果:
49124a83-01d7-4d0a-87ca-a6adda251461
去掉“-”后:
49124a8301d74d0a87caa6adda251461
--------------------------------------------------------------------------
14.在给定的字符串“ABCDEFGhijklmn1234567”中随机获取4个字符,并使用StringBuilder拼接成字符串
import java.util.Random;
public class Test {
public static void main(String[] args) {
String str = "ABCDEFGhijklmn1234567";
Random random = new Random();
StringBuilder newStr = new StringBuilder();
for(int i=0;i<4;i++) {
newStr = newStr.append(str.charAt(random.nextInt(str.length())));
}
System.out.println("在"+str+"中");
System.out.println("随机选取4个字符组成字符串:"+newStr);
}
}
运行结果:
在ABCDEFGhijklmn1234567中
随机选取4个字符组成字符串:mC25
--------------------------------------------------------------------------
15.给定一个由数字组成的字符串,统计每个数字出现的次数
public class Test {
public static void main(String[] args) {
String str = "951357842160201490324870168";
char[] arr = str.toCharArray();
System.out.println("在" + str + "中有:");
for (int i = 0; i <= 9; i++) {
int count = 0;
for (int j = 0; j < arr.length; j++) {
if (i == arr[j] - 48) {
count++;
}
}
System.out.println(count + " 个 " + i);
}
}
}
运行结果:
在951357842160201490324870168中有:
4 个 0
4 个 1
3 个 2
2 个 3
3 个 4
2 个 5
2 个 6
2 个 7
3 个 8
2 个 9