package test;
public class test1 {
public static void main(String[] args) {
String str1 = new String();
String str2 = new String("abcd");
char[] chars = new char[]{'d', 'e', 'f'};
String str3 = new String(chars);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
package test;
public class test2 {
public static void main(String[] args) {
basic();
alter();
logic();
}
public static void basic() {
String str = "Life is a chain of moments of enjoyment, not only about survival";
System.out.println("字符串的长度:" + str.length());
System.out.println("第一个字符为:" + str.charAt(1));
System.out.println("字符c第一次出现的位置为:" + str.indexOf("c"));
System.out.println("字符c最后一次出现的位置为:" + str.lastIndexOf('c'));
System.out.println("字符串enjoy第一次出现的位置为:" + str.indexOf("enjoy"));
System.out.println("字符串enjoy最后一次出现的位置是:" + str.lastIndexOf("we"));
}
public static void alter() {
String str = "To the world you may be one person, but to one person you may be the world";
System.out.println("字符串转换成字符数组后的结果:");
char[] charArray = str.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.println(charArray[i] + ",");
}
System.out.println("字符串转换为大写的结果:" + str.toUpperCase());
System.out.println("将but用however代替:" + str.replace("but", "however"));
System.out.println("将the前后空隔去除:" + str.trim());
}
public static void logic() {
String str1 = "String";
String str2 = "Str";
System.out.println("判断字符串的开头是否为Str:" + str1.startsWith("Str"));
System.out.println("判断字符串的结尾是否为ng:" + str1.endsWith("ng"));
System.out.println("判断字符串是否包含in:" + str1.contains("in"));
System.out.println("判断字符串是否为空:" + str1.isEmpty());
System.out.println("判断两个字符串是否相等:" + str1.equals(str2));
}
}
package test;
public class test3 {
public static void main(String[] args) {
String str = "羽毛球-篮球-乒乓球";
System.out.println("从第五个字符截取到末尾的结果:" + str.substring(4));
System.out.println("从第五个到第六个字符的截取的结果:" + str.substring(4, 6));
String[] strs = str.split("-");
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i] + ",");
}
}
}
package test;
public class test4 {
public static void main(String[] args) {
add();
alter();
remove();
}
public static void add() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("abc");
System.out.println("append添加结果为:" + stringBuffer);
stringBuffer.insert(2, "123");
System.out.println("insert添加结果为:" + stringBuffer);
}
public static void remove() {
StringBuffer stringBuffer = new StringBuffer("abcdefg");
System.out.println("删除范围1至5的结果:" + stringBuffer.delete(1, 5));
System.out.println("删除位置2的结果:" + stringBuffer.deleteCharAt(2));
System.out.println("清空结果:" + stringBuffer.delete(0, stringBuffer.length()));
}
public static void alter() {
StringBuffer stringBuffer = new StringBuffer("abcdef");
stringBuffer.setCharAt(1, 'p');
System.out.println("修改指定位置结果:" + stringBuffer);
stringBuffer.replace(1, 3, "q999q");
System.out.println("替换指定位置字符(串)结果:" + stringBuffer);
System.out.println("字符串反转结果为:" + stringBuffer.reverse());
}
}
package test;
public class test5 {
public static void main(String[] args) {
addTo();
}
public static void equalsTo() {
String str1 = new String("abc");
String str2 = new String("abc");
System.out.println(str1.equals(str2));
StringBuffer sb1 = new StringBuffer("abc");
StringBuffer sb2 = new StringBuffer("abc");
System.out.println(sb1.equals(sb2));
}
public static void addTo() {
String s1 = "a";
String s2 = "b";
String s3 = s1 + s2;
System.out.println(s3);
StringBuffer sb1 = new StringBuffer("a");
StringBuffer sb2 = new StringBuffer("b");
}
}