我是靠谱客的博主 无辜绿草,这篇文章主要介绍2. String 类的常用方法练习。阅读分析下面程序,把程序中的代码补充完整,并查看结果,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2. String 类的常用方法练习。阅读分析下面程序,把程序中的代码补充完整,并查看结果 public class StringExample { public static void main(String args[]) { String s1 = new String("you are a student"), s2 = new String("how are you"); if (【补充代码】) // 使用 equals 方法判断 s1 与 s2 是否相同 { System.out.println("s1 与 s2 相同"); } else { System.out.println("s1 与 s2 不相同"); } String s3 = new String("43010219851022024"); if (【补充代码】) // 判断 s3 的前缀是否是“4301” { System.out.println("湖南省的身份证"); } String s4 = new String("你"), s5 = new String("我"); if (【补充代码】)// 按着字典序 s4 大于 s5 的表达式 { System.out.println("按字典序 s4 大于 s5"); } else { System.out.println("按字典序 s4 小于 s5"); } int position = 0; String path = "c:\java\A.java"; position = 【补充代码】// 获取 path 中最后出现\的位置 System.out.println("c:\java\A.java 中最后出现\的位置:" + position); String fileName = 【补充代码】// 获取 path 中“A.java”子字符串 System.out.println("c:\javaA.java 中含有的文件名:" + fileName); String s6 = new String("100"), s7 = new String("123.678"); int n1 = 【补充代码】 // 将 s6 转化成 int 型数据 double n2 = 【补充代码】 // 将 s7 转化成 double 型数据 double m = n1 + n2; System.out.println(m); String s8 = 【补充代码】 // String 调用 valuOf(double n)方法将 m 转化为字符串对象 position = s8.indexOf("."); String temp = 【补充代码】 // 获取 s8 中小数点后面的小数 System.out.println("数字" + m + "有" + temp.length() + "位小数"); String s9 = new String("ABCDEF"); char a[] = 【补充代码】 // 将 s9 存放到数组 a 中 for (int i = a.length - 1; i >= 0; i--) { System.out.print(" " + a[i]); } } }

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.temp; /** * @Author lanxiaofang * @email 983770299@qq.com * @date 2020109/21 9:55 */ public class PracticeString { public static void main(String[] args) { String s1 = new String("you are a student"), s2 = new String("how are you"); if (s1.equals(s2)) // 使用 equals 方法判断 s1 与 s2 是否相同 { System.out.println("s1 与 s2 相同"); } else { System.out.println("s1 与 s2 不相同"); } String s3 = new String("43010219851022024"); if (s3.substring(0,4).equals("4301")) // 判断 s3 的前缀是否是“4301” { System.out.println("湖南省的身份证"); } String s4 = new String("你"), s5 = new String("我"); if (s4.compareTo(s5)>0)// 按着字典序 s4 大于 s5 的表达式 { System.out.println("按字典序 s4 大于 s5"); } else { System.out.println("按字典序 s4 小于 s5"); } int position = 0; String path = "c:\\java\\A.java"; //注意这个位置要转义\,\结果是 position = path.lastIndexOf("\");// 获取 path 中最后出现\的位置 System.out.println("c:\\java\\A.java 中最后出现\\的位置:" + position); String fileName = path.substring(position+1);// 获取 path 中“A.java”子字符串 System.out.println("c:\java\A.java 中含有的文件名:" + fileName); String s6 = new String("100"), s7 = new String("123.678"); int n1 = Integer.parseInt(s6);// 将 s6 转化成 int 型数据 double n2 = Double.parseDouble(s7); // 将 s7 转化成 double 型数据 double m = n1 + n2; System.out.println(m); String s8 = String.valueOf(m); // String 调用 valuOf(double n)方法将 m 转化为字符串对象 position = s8.indexOf("."); String temp = s8.substring(position+1); // 获取 s8 中小数点后面的小数 System.out.println("数字" + m + "有" + temp.length() + "位小数"); String s9 = new String("ABCDEF"); char a[] = s9.toCharArray(); // 将 s9 存放到数组 a 中 for (int i = a.length - 1; i >= 0; i--) { System.out.print(" " + a[i]); } } }

 

 

 

最后

以上就是无辜绿草最近收集整理的关于2. String 类的常用方法练习。阅读分析下面程序,把程序中的代码补充完整,并查看结果的全部内容,更多相关2.内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部