1.charAt(int index) 返回指定索引的 char值
char c = "中国人" . charAt ( 1 ) ;
System . out. println ( c) ;
2.int compareTo(String anotherString) 比较两个字符串的字典
int result = "abc" . compareTo ( "abc" ) ;
System . out. print ( "" + result) ;
int result4 = "cdab" . compareTo ( "abcf" ) ;
System . out. print ( " " + result4) ;
3.boolean contains(CharSequenec s) 判断前面字符串是否包含后面的子字符串
System . out. println ( "acv" . contains ( "a" ) ) ;
4.boolean endsWith(String suffix) 判断当前字符串是否以某个字符串结尾
System . out. println ( "acv" . endsWith ( "v" ) ) ;
5.boolean equals(Object anObject) 比较两个字符串
System . out. println ( "acd" . equals ( "acd" ) ) ;
6.boolean equalsIgnoreCase(String anotherString) 判断两个字符串是否相等忽略大小写
System . out. println ( "ACD" . equals ( "acd" ) ) ;
7.byte[] getBytes() 将字符串对象转换成字节数组
byte [ ] bytes = "abcdef" . getBytes ( ) ;
for ( int i = 0 ; i < bytes. length; i++ ) {
System . out. print ( bytes[ i] + " " ) ;
}
System . out. println ( ) ;
8.int indexOf(String str) 判断某个字符串在当前字符串中第一次出现的索引
System . out. println ( "adsada" . indexOf ( "s" ) ) ;
9.boolean isEmpty() 判断某个字符串是否为空
String s = null ;
String s = "" ;
System . out. println ( s. isEmpty ( ) ) ;
10.int length() 获取字符串长度
int [ ] a = { 1 , 2 , 3 } ;
System . out. println ( a. length) ;
System . out. println ( "abc" . length ( ) ) ;
11.int lastIndexOf(String str) 判断某个子字符串在当前字符串最后一次出现的索引
System . out. println ( "abca" . lastIndexOf ( "a" ) ) ;
12.String replace(CharSequence target, CharSequence replacement) 字符串替换
System . out. println ( "http://www.baidu.com" . replace ( "http://" , "https://" ) ) ;
13.String[] split(String regex) 拆分字符串
String [ ] a1 = "1980-10-11" . split ( "-" ) ;
for ( int i = 0 ; i < a1. length; i++ ) {
System . out. print ( a1[ i] + " " ) ;
}
14.boolean startsWith(String prefix) 判断某个字符串是否以某个子字符串开始
System . out. println ( "abcfdf" . startsWith ( "a" ) ) ;
15.String substring(int beginIndex) 截取字符串
System . out. println ( "abcdefghijk" . substring ( 2 ) ) ;
16.String substring(int beginIndex, int endIndex) 截取字符串
System . out. println ( "abcdefghijk" . substring ( 2 , 7 ) ) ;
17.char[] toCharArray() 将字符串转换为char数组
char [ ] chars = "我是中国人" . toCharArray ( ) ;
for ( int i = 0 ; i < chars. length; i++ ) {
System . out. print ( chars[ i] + " " ) ;
}
18.String toLowerCase() 转换为小写
System . out. println ( "ABC" . toLowerCase ( Locale . ROOT) ) ;
19.String toUpperCase() 转换为大写
System . out. println ( "abc" . toUpperCase ( Locale . ROOT) ) ;
20.String trim() 去除字符串前后空白
System . out. println ( " a b c " . trim ( ) ) ;
21.valueOf() 将非字符串转为字符串
public class test {
public static void main ( String [ ] args) {
Student student = new Student ( ) ;
student. name = "zhangsan" ;
student. age = 18 ;
System . out. println ( String . valueOf ( student) ) ;
}
}
class Student {
String name;
int age;
@Override
public String toString ( ) {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}' ;
}
}