Java-字符串-String类

1 需求

1.1 Field Summary


1.2 Constructor Summary

  • public String() : 空构造
  • public String(byte[] bytes) : 把字节数组转成字符串
  • public String(byte[] bytes,int index, int length) : 把字节数组的一部分转成字符串
  • public String(char[] value) : 把字符数组转成字符串
  • public String(char[] value,int index, int count) : 把字符数组的一部分转成字符串

1.3 Method Summary

  • 判断字符串是否为空
    • public boolean isEmpty()
  • 获取字符串的长度
    • public int length()
  • 判断字符串是否相等
    • public boolean equals(Object anObject)
    • public boolean contentEquals(StringBuffer sb)
    • public boolean contentEquals(CharSequence cs)
    • public boolean equalsIgnoreCase(String anotherString)
  • 判断是否包含子字符串
    • public boolean contains(CharSequence s)
    • public boolean matches(String regex)
  • ……
    • public boolean startsWith(String prefix, int toffset)
    • public boolean startsWith(String prefix)
  • ……
    • public boolean endsWith(String suffix)
       
  • 分割字符串
    • public String[] split(String regex)
    • public String[] split(String regex, int limit)
  • ……
    • public int indexOf(int ch)
    • public int indexOf(int ch, int fromIndex)
    • public int indexOf(String str)
    • public int indexOf(String str, int fromIndex)
  • 判断某个字符最后出现的位置
    • public int lastIndexOf(int ch)
    • public int lastIndexOf(int ch, int fromIndex)
    • public int lastIndexOf(String str)
    • public int lastIndexOf(String str, int fromIndex)
  • 字符串替换
    • public String replace(char oldChar, char newChar)
    • public String replace(CharSequence target, CharSequence replacement)
    • public String replaceFirst(String regex, String replacement)
    • public String replaceAll(String regex, String replacement)
  • 字符串查找
    • public String substring(int beginIndex)
    • public String substring(int beginIndex, int endIndex)
    • public CharSequence subSequence(int beginIndex, int endIndex)

2 接口


3.X 示例:字符串定义(两种方式)

public class Test {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = new String("hello");

        System.out.println(s1 == s2);
    }
}

Java-字符串-String类_第1张图片

参考资料

JAVA字符串的两种定义方式的区别_java中字符串的定义方式及区别-CSDN博客


3.X 示例:未初始化报错

public class Test {
    public static void main(String[] args) {
        String s1;

        System.out.println(s1);
    }
}

Java-字符串-String类_第2张图片


3.X 示例:空字符串

public class Test {
    public static void main(String[] args) {
        String s1 = "";
        System.out.println(s1.isEmpty());
        System.out.println(s1.length());
    }
}

Java-字符串-String类_第3张图片


3.X 示例:null字符串

public class Test {
    public static void main(String[] args) {
        String s1 = null;
        System.out.println(s1.isEmpty());
    }
}

Java-字符串-String类_第4张图片

public class Test {
    public static void main(String[] args) {
        String s1 = null;
        System.out.println(s1.length());
    }
}

Java-字符串-String类_第5张图片


4 参考资料

java基础-String_public string{byte[] bytes,-CSDN博客

你可能感兴趣的:(Java-入门教程,Java,字符串,String)