package demo01;
public class Demo01String {
public static void main(String[] args) {
String str1 = new String();
System.out.println("第1个字符串是:" + str1);
char[] charArray = {'A','B','C',};
String str2 = new String(charArray);
System.out.println("第2个字符串是:" + str2);
byte[] byteArray = {97,98,99};
String str3 = new String(byteArray);
System.out.println("第3个字符串是:" + str3);
String str4 = "Hello Word!";
System.out.println("第4个字符串是:" + str4);
}
}
package demo02;
public class Demo01StringEquals {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
char[] charArray = {'H','e','l','l','o'};
String str3 = new String(charArray);
System.out.println(str1.equals(str2));
System.out.println(str2.equals(str3));
System.out.println(str3.equals("Hello"));
System.out.println("Hello".equals(str1));
String str4 = "hello";
System.out.println(str1.equals(str4));
String str5 = "abc";
System.out.println("abc".equals(str5));
System.out.println(str5.equals("abc"));
System.out.println("===================");
String strA = "Java";
String strB = "java";
System.out.println(strA.equals(strB));
System.out.println(strA.equalsIgnoreCase(strB));
}
}
package demo02;
public class Demo02StringGet {
public static void main(String[] args) {
int length = "ahavnahgalgjajgvja".length();
System.out.println("字符串的长度是:" + length);
String str1 = "Hello";
String str2 = "World";
String str3 = str1.concat(str2);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println("=============");
char ch = "Hello".charAt(1);
System.out.println("在1号索引位置的字符串是:" + ch);
System.out.println("=============");
String original = "HelloWorld";
int index = original.indexOf("llo");
System.out.println("第一次索引值是:" + index);
System.out.println("HelloWorld".indexOf("abc"));
}
}
package demo02;
public class Demo03Substring {
public static void main(String[] args) {
String str1 = "HelloWorld";
String str2 = str1.substring(5);
System.out.println(str1);
System.out.println(str2);
System.out.println("=========");
String str3 = str1.substring(4,7);
System.out.println(str3);
System.out.println("=========");
String strA = "Hello";
System.out.println(strA);
strA = "Java";
System.out.println(strA);
}
}
package demo02;
public class Demo04StringConvert {
public static void main(String[] args) {
char[] chars = "Hello".toCharArray();
System.out.println(chars[0]);
System.out.println(chars.length);
System.out.println("===========");
byte[] bytes = "abc".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
System.out.println("============");
String str1 = "How do you do?";
String str2 = str1.replace("o","*");
System.out.println(str1);
System.out.println(str2);
System.out.println("===========");
String lang1 = "会不会玩啊!你大爷的!你大爷的!";
String lang2 = lang1.replace("你大爷的", "****");
System.out.println(lang2);
}
}
package demo02;
public class Demo05StringSplit {
public static void main(String[] args) {
String str1 = "aaa,bbb,ccc";
String[] array1 = str1.split(",");
for (int i = 0; i < array1.length; i++) {
System.out.println(array1[i]);
}
System.out.println("============");
String str2 = "aaa bbb ccc";
String[] array2 = str2.split(" ");
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i]);
}
System.out.println("============");
String str3 = "XXX.YYY.ZZZ";
String[] array3 = str3.split("\\.");
for (int i = 0; i < array3.length; i++) {
System.out.println(array3[i]);
}
}
}