Java String包装类常用方法
- Java String包装类常用方法
- String中方法的介绍
- public char charAt(int index)
- public int compareTo(String anotherString)
- public int compareToIgnoreCase(String str)
- public String concat(String str)
- public boolean contains(CharSequence s)
- public boolean startsWith(String prefix)
- public boolean endsWith(String suffix)
- public boolean equals(Object anObject)
- public boolean equalsIgnoreCase(String anotherString)
- public static String format(String format, Object... args)
- public int indexOf(String str)
- public int lastIndexOf(String str)
- public int length()
- public String replace(char oldChar, char newChar)
- public String replace(CharSequence target, CharSequence replacement)
- public String[] split(String regex)
- public String substring(int beginIndex)
- public char[] toCharArray()
- public String toLowerCase()
- public String toUpperCase()
- public String trim()
- public static String valueOf()
Java String包装类常用方法
String中方法的介绍
public char charAt(int index)
返回指定索引处的 char 值
public int compareTo(String anotherString)
按字典顺序比较两个字符串
返回值的解释
返回值等于零:两个字符串相等
返回值小于零:字符字典顺序比参数字符串前
返回值大于零:字符字典顺序比参数字符串后
public int compareToIgnoreCase(String str)
按字典顺序比较两个字符串,不考虑大小写
public String concat(String str)
类似于加号,将指定字符串连接到此字符串的结尾
public boolean contains(CharSequence s)
当此字符串包含指定的 char 值序列时,返回 true
public static void main(String[] args) {
String str = "abcefg";
System.out.println(str.contains("fg"));//返回true
}
public boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。
public static void main(String[] args) {
String str = "abcefg";
System.out.println(str.startsWith("abc"));//返回true
}
public boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结束。
public boolean equals(Object anObject)
将此字符串与指定的对象比较。
需要注意 “==” 比较的是地址,也就是是否为同一个对象
public boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 比较,不考虑大小写。
public static String format(String format, Object… args)
格式化字符串
public int indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引,如果没有找到,返回-1
public int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引
public int length()
返回此字符串的长度
public String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的
public String replace(CharSequence target, CharSequence replacement)
使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串
public String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串
public String substring(int beginIndex)
返回一个新字符串,它是此字符串的一个子字符串。
public char[] toCharArray()
将此字符串转换为一个新的字符数组
public String toLowerCase()
转换为小写
public String toUpperCase()
转换为大写
public String trim()
返回字符串的副本,忽略前导空白和尾部空白
public static String valueOf()
把其他类型转换成字符串类型