String类

    技术2022-07-11  122

    String类的方法都有那些,主要分为四个部分:判断,转换,获取和其他功能。 判断功能:

    boolean equals(Object obj):判断两个字符串是否相同 boolean equalsIgnoreCase(String str):比较字符串是否想同忽略大小写 boolean contains(String str):判断大字符串中是否包含小字符串 boolean startWith(String str):判断字符串是否以某个指定字符串开头 boolean endWith(String str):判断字符串是否以某个指定字符串结尾 boolean isEmpty():判断是否为空(内容为空和字符串对象为空是两个东西)

    转换功能:

    byte []getBytes():把字符串转换成字节数组 char [] toCharArray():把字符串转换成字符数组 static String valueOf(char[] chs):把字符数组转换成字符串 static String valueOf(int i):把int类型数据转换成字符串 注意:String类的valueOf()方法可以把任意类型数据转换成字符串 String toLowerCase():把字符串转换小写 String toUpperCase():把字符串转换大写 String concat(String str):拼接字符串

    获取功能:

    int length():获取字符串长度 char charAt(int index):获取指定所引出字符 int indexOf(int ch):返回指定字符在字符串中第一次出现的索引 为什么是int类型而不是char类型 原因是:‘a’和97都表示‘a’ int indexOf(String str):返回指定字符串在字符串中第一次出现的索引 int indexOf(int ch,int fromIndex):返回指定字符在字符串中从指定位置之后第一次出现的索引 int indexOf(String str,int fromIndex):返回指定字符串在字符串中从指定位置之后第一次出现的索引 String substring(int start):从指定位置截取字符串默认到末尾 String substring(int start,int end):从指定位开始到指定位置结束置截取字符串

    替换功能:

    String replace(char old,char new) String replace(String old,String new) 去除字符串两端空格 String trim() 按字典顺序比较两个字符 int compareTo(String str) int compareToIgnoreCase(String str)
    Processed: 0.011, SQL: 9