老师 您好String的作业我完成了下,请老师您检查下
来源:9-13 测评作业

unbreakable_全栈
2024-11-26
public class WorkStringDemo {
public static void main(String[] args) {
// 1、帮小慕分析一下,以下字符串的比较结果
work1();
// 2、练习:在 API 帮助文档中,查找字符串的 startsWith 和 endsWith 方法
work2();
// 3、将手机号码中间的四位数字替换成 * 号
work3();
// 4、遍历字符串数组,将数组元素添加到可变字符串中
// 遍历字符串数组,将数组中的诗句添加到可变字符串中,实现以下效果图
// String poemArr[] = {
// "青青园中葵, 朝露待日晞。",
// "阳春布德泽,万物生光辉。",
// "常恐秋节至,焜黄华叶衰。",
// "百川东到海,何时复西归?",
// "少壮不努力,老大徒伤悲。"
//};
work4();
}
// 1、帮小慕分析一下,以下字符串的比较结果
public static void work1 () {
// 比较username和nickname
String username = new String("xiaomu");
String nickname = new String("xiaomu");
System.out.println("username == nickname => " + (username == nickname)); // false
System.out.println("username.equals(nickname) => " + username.equals(nickname)); // true
// 比较password和rePassword
String password = "abc123";
String rePassword = "abc123";
System.out.println("password == rePassword => " + (password == rePassword)); // true
System.out.println("password.equals(rePassword) => " + password.equals(rePassword)); // true
}
// 2、练习:在 API 帮助文档中,查找字符串的 startsWith 和 endsWith 方法
public static void work2 () {
String str = "Hello, World!";
// 使用 startsWith 方法检查字符串是否以 "Hello" 开头
boolean startsWithHello = str.startsWith("Hello");
System.out.println("Does the string start with 'Hello'? " + startsWithHello); // true
// 使用 startsWith 方法检查字符串是否以 "world"(不区分大小写)开头
boolean startsWithWorldIgnoreCase = str.startsWith("world", 7);
System.out.println("Does the string start with 'world' (case insensitive) at index 7? " + startsWithWorldIgnoreCase); // false
// 使用 endsWith 方法检查字符串是否以 "World!" 结尾
boolean endsWithWorld = str.endsWith("World!");
System.out.println("Does the string end with 'World!'? " + endsWithWorld); // true
}
// 3、将手机号码中间的四位数字替换成 * 号
// 将手机号码 13512345566 中间的四位数字替换成 * 号,实现以下效果图
public static void work3 () {
// 定义手机号码
String phoneNumber = "13512345566";
// 检查手机号码长度是否为11位
if (phoneNumber.length() == 11) {
// 获取前三位
String firstPart = phoneNumber.substring(0, 3);
// 获取后四位
String lastPart = phoneNumber.substring(7);
// 中间四位用 **** 替换
String maskedPhoneNumber = firstPart + "****" + lastPart;
// 输出结果
System.out.println("原始手机号: " + phoneNumber); // 13512345566
System.out.println("处理后的手机号: " + maskedPhoneNumber); // 135****5566
} else {
System.out.println("手机号码长度不正确,请输入11位的手机号码。");
}
}
// 4、遍历字符串数组,将数组元素添加到可变字符串中
public static void work4 () {
// 定义字符串数组
String poemArr[] = {
"青青园中葵, 朝露待日晞。",
"阳春布德泽,万物生光辉。",
"常恐秋节至,焜黄华叶衰。",
"百川东到海,何时复西归?",
"少壮不努力,老大徒伤悲。"
};
// 创建一个 StringBuilder 对象
StringBuilder poemBuilder = new StringBuilder();
// 遍历字符串数组,将每个元素添加到 StringBuilder 中
for (String line : poemArr) {
poemBuilder.append(line).append("\n"); // 添加换行符以分隔每行诗句
}
// 将 StringBuilder 转换为字符串
String completePoem = poemBuilder.toString();
// 输出结果
System.out.println(completePoem);
}
}
写回答
1回答
-
彭彭老师
2024-11-28
第2题,检查字符串是否以“world”开头(不区分大小写),使用 equalsIgnoreCase() 方法;
两个参数的 startsWith 方法,表示从指定的索引位置开始,判断子字符串是否以指定的内容开头
// 使用 startsWith 方法检查字符串是否以 "world"(不区分大小写)开头 boolean startsWithWorldIgnoreCase = str.equalsIgnoreCase("hello"); System.out.println("Does the string start with 'hello' (case insensitive) " + startsWithWorldIgnoreCase); // true // 从指定的索引位置开始,判断字符串的子字符串是否以指定的前缀 prefix 开头 // toffset,表示从哪里开始查找这个子字符串 boolean startsWithWorldToffset = str.startsWith("World", 7); System.out.println("Does the string start with 'World' (case insensitive) at index 7? " + startsWithWorldToffset); // true
第4题,可以直接打印 StringBuilder 字符串,会自动调用 toString() 方法
// 4、遍历字符串数组,将数组元素添加到可变字符串中 public static void work4 () { // 定义字符串数组 String poemArr[] = { "青青园中葵, 朝露待日晞。", "阳春布德泽,万物生光辉。", "常恐秋节至,焜黄华叶衰。", "百川东到海,何时复西归?", "少壮不努力,老大徒伤悲。" }; // 创建一个 StringBuilder 对象 StringBuilder poemBuilder = new StringBuilder(); // 遍历字符串数组,将每个元素添加到 StringBuilder 中 for (String line : poemArr) { poemBuilder.append(line).append("\n"); // 添加换行符以分隔每行诗句 } // 将 StringBuilder 转换为字符串 // String completePoem = poemBuilder.toString(); // 输出结果 // System.out.println(completePoem); System.out.println(poemBuilder); }
00
相似问题
array的作业,请老师您检查下
回答 1
第10章作业提交
回答 1