package com.asia.sun.demo5;
public class String01 {
public static void main(String[] args) {
// 连接字符串
String s1 = new String("hello");
String s2 = new String("world");
String s = s1 + " " + s2;
System.out.println(s);
System.out.println("-----------------------------");
int look = 5;
float practice = 2.56f;
System.out.println("我每天花费" + look + "个小时看书," + practice + "个小时做上机练习");
System.out.println("-------------------------------------");
// 输出字符串中索引位置的字符
String str = "we are students";
int size = str.lastIndexOf(" ");
System.out.println("空字符串在字符串中的索引位置是" + size);
System.out.println("字符串的长度" + str.length());
System.out.println("--------------------------------------");
char mychar = s.charAt(7);
System.out.println("-------------" + mychar);
System.out.println("--------------------------------------");
// 截取字符串
String i = s.substring(1, 5);
System.out.println(i);
System.out.println("--------------------------------------");
// 输出去掉空格后字符串的长度
System.out.println("剔除空格后字符串的长度:" + s.trim().length());
System.out.println("--------------------------------------");
// 替换字符串中的字符
String str1 = s.replace("e", "E");
System.out.println("替换字符后的字符串:" + str1);
System.out.println("--------------------------------------");
// 判断字符串的开头、结尾字符
boolean b = s.startsWith("h");
boolean b1 = s.endsWith("e");
System.out.println("字符串是以h开头的吗?" + b);
System.out.println("字符串是以e结尾的吗?" + b1);
System.out.println("--------------------------------------");
// 字符串的比较
String s01 = new String("abc");
String s02 = new String("ABC");
String s03 = new String("AbC");
boolean b01 = s01.equals(s02);
// 两字符串忽略大小写比较
boolean b02 = s01.equalsIgnoreCase(s02);
System.out.println("s01 equals s02:" + b01);
System.out.println("s01 equalsIgnoreCase s02:" + b02);
System.out.println("输出s01与s02比较的结果:" + s01.compareTo(s02));
System.out.println("--------------------------------------");
// 字符串大小写转换
String s011 = s01.toUpperCase();
String s101 = s02.toLowerCase();
System.out.println("s01转化成大写字母" + s011);
System.out.println("s02转化成小写字母" + s101);
System.out.println("--------------------------------------");
// 对字符串进行拆分
String tr = new String("abc,def,hij,klm");
// 使用split()方法对字符串进行拆分,返回字符串数组
String[] newstr = tr.split(",");
for (int j = 0; j < newstr.length; j++) {
System.out.println(newstr[j]);
}
System.out.println("--------------------------------------");
// 对字符串进行拆分,并限定拆分次数
String[] newstr2 = tr.split(",", 2);
for (int j = 0; j < newstr2.length; j++) { // 循环遍历字符数组
System.out.println(newstr2[j]); // 输出信息
}
System.out.println("--------------------------------------");
}
}