String类的常用方法练习,补充代码并查看结果
public class hello {
/*
Author @liwei
email @1525615956@qq.com
data @2020.9.21
*/
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.startsWith("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]);
}
}
}
运行结果为:
s1 与 s2 不相同
湖南省的身份证
按字典序 s4 小于 s5
c:\java\A.java 中最后出现\的位置:7
c:\java\A.java 中含有的文件名:A.java
223.678
数字223.678有3位小数
F E D C B A
本题用了许多的string类的函数,可以多加练习熟练运用