初始化(构造函数)
String str1 = new String();
System.out.println(str1);
char[] arr = {'a', 'b', 'c'};
String str2 = new String(arr);
System.out.println(str2);
String str3 = "zhang";
System.out.println(str3);
String str4 = new String("xuezhi");
System.out.println(str4);
String方法
str.charAt()
String str1 = new String("hello, world!");
System.out.println(str1.charAt(4));
str.codePointAt()
public static void main (String[] args) {
String str1 = new String("hello, world!");
System.out.println(str1.codePointAt(4));
System.out.println(str1.codePointAt(8));
str.codePointBefore()
String str1 = new String("hello, world!")
System.out.println(str1.codePointBefore(3))
System.out.println(str1.codePointBefore(4))
str.codePointCount()
String str1 = new String("hello, world!");
System.out.println(str1.codePointCount(0, 2));
str.compareTo()
String str1 = "ba"
String str2 = "ba"
String str3 = "be"
System.out.println(str1.compareTo(str2))
System.out.println(str1.compareTo(str3))
str.compareToIgnoreCase()
- 不区分大小形式,比较两个字符串的字典序,相等时返回0
String str1 = "ba"
String str2 = "bE"
System.out.println(str1.compareTo(str2))
// 不区分大小写
System.out.println(str1.compareToIgnoreCase(str2))
str.concat()
String str1 = "hello ";
String str2 = "world!";
String str3 = str1.concat(str2);
String str4 = str1 + str2;
System.out.println(str3);
System.out.println(str4);
str.contains()
String str1 = "hello "
String str2 = "ll"
String str3 = "ab"
System.out.println(str1.contains(str2))
System.out.println(str1.contains(str3))
str.contentEquals()
String str1 = "hello "
String str2 = "ll"
StringBuffer str3 = new StringBuffer("hello ")
System.out.println(str1.contentEquals(str2))
System.out.println(str1.contentEquals(str3))
str.copyValueOf()
char[] arr = {'h', 'e', 'l', 'l', 'o'}
String str1 = String.copyValueOf(arr)
String str2 = String.copyValueOf(arr, 2, 2)
System.out.println(str1)
System.out.println(str2)
str.endsWith()
String str1 = "hello, world"
System.out.println(str1.endsWith("world"))
str.equals() & str.equalsIgnoreCase()
String str1 = "hello, world"
String str2 = "Hello, world"
System.out.println(str1.equals(str2))
System.out.println(str1.equalsIgnoreCase(str2))
String str1 = String.format("Hi, %s!", "Xuezhi")
String str2 = String.format("价格:%d元", 4)
String str3 = String.format("价格:%.2f元", 4.5)
System.out.println(str1)
System.out.println(str2)
System.out.println(str3)
str.getBytes()
String str1 = "abcdef";
byte[] bArr = str1.getBytes();
for (byte item : bArr)
System.out.print(item + " ");
str.hashCode()
String str1 = "abcdef"
System.out.println(str1.hashCode())
str.indexOf()
String str1 = "abcabc"
System.out.println("a的位置是:" + str1.indexOf("a"))
System.out.println("a的位置是:" + str1.indexOf('a'))
System.out.println("a的位置是:" + str1.indexOf("a", 2))
str.lastIndexOf()
String str1 = "abcabc"
System.out.println("a的最后位置是:" + str1.lastIndexOf("a"))
System.out.println("a的最后位置是:" + str1.lastIndexOf('a'))
System.out.println("a的最后位置是:" + str1.lastIndexOf("a", 2))
str.intern()
String str1 = "abcabc"
String str2 = str1.intern()
System.out.println(str2)
str.isEmpty()
String str1 = "abc"
String str2 = ""
System.out.println(str1.isEmpty())
System.out.println(str2.isEmpty())
str.length()
String str1 = "abc"
String str2 = ""
System.out.println(str1.length())
System.out.println(str2.length())
str.matches()
String str1 = "hello, world"
System.out.println(str1.matches("(.*)world(.*)"))
str.replace()
String str1 = "hello, world"
String str2 = str1.replace('h', 'H')
String str3 = str1.replace("world", "WORLD")
System.out.println(str2)
System.out.println(str3)
str.replaceAll()
String str1 = "hello, world";
String str2 = str1.replaceAll("l", "L");
System.out.println(str2);
str.replaceFirst()
String str1 = "hello, world"
String str2 = str1.replaceFirst("l", "L")
System.out.println(str2)
str.split()
String str1 = "abcabcabcabc"
String[] strArr = str1.split("a")
System.out.println(strArr.length)
for (String item : strArr)
System.out.println(item)
String[] strArr2 = str1.split("a",3)
System.out.println(strArr2.length)
for (String item : strArr2)
System.out.println(item)
str.startsWith()
String str1 = "hello, world!"
System.out.println(str1.startsWith("hello"))
System.out.println(str1.startsWith("world", 7))
str.subSequence()
String str1 = "hello, world!"
System.out.println(str1.subSequence(7, 12))
str.substring()
String str1 = "hello, world!"
System.out.println(str1.substring(7, 12))
System.out.println(str1.substring(7))
str.toCharArray()
String str1 = "hello";
char[] charArr = str1.toCharArray();
for (char item : charArr)
System.out.print(item + " ");
str.toLowerCase()
String str1 = "abc DEF"
System.out.println(str1.toLowerCase())
str.toUpperCase()
String str1 = "abc DEF"
System.out.println(str1.toUpperCase())
str.trim()
String str1 = " abc def "
System.out.println(str1.trim())
str.valueOf()
char[] charArr = {'a', 'b', 'c'}
System.out.println(String.valueOf(false))
System.out.println(String.valueOf('a'))
System.out.println(String.valueOf(charArr))
System.out.println(String.valueOf(4.5))
System.out.println(String.valueOf(4))