1、lastIndexOf("参数") 查找字符或者子串是后一次出现的地方
字符串出现的最后一个位置
例:String a = "abcdefgfh";
lastIndexOf是字符串出现的最后一个位置
例:a.lastIndexOf("f")就是7而不是5
2、substring("参数一","参数二")截取字符串
"hamburger".substring(4,8) // urge
"smiles".substring(1,5) //mile
注:1,两个S都要小写!
2,属于String对象的方法。
3,索引下标从0开始
4,返回一个从start开始到end的字符串,但不包含end索引位置的字符。
substring("参数")
例:String zf="abcdef.jpg";
System.out.println(zf.lastIndexOf("."));
System.out.println(zf.substring(zf.lastIndexOf(".")));
执行结果:6
.jpg
注:
zf.lastIndexOf("."):zf字符串从0开始。搜索到字符串最后一个出现“.”的位置,返回值为 int
例子中“点”出现的位置为:6
zf.substring("6"):是截取字符串中从“6”开始直到字符串末端
3、concat() 连接两个字符串
一、String s = "Welcome to ";
String t=s.concat("AnHui");
System.out.println(t);
二、String s = "Welcome to ";
String t= "hrb";
String st = s.concat(t);
System.out.println(st);
4、String replace(char original,char replacement) 字符串替换
替换指定的字符,全部替换
例:String s="Hello".replace('l','w');
System.out.println(s);
执行结果:Hewwo
5、valueOf() 转换为字符串
例:int i = 1 ;
String s = String.valueOf(i);
System.out.println(s);
执行结果:1
6、toLowerCase() 转换为小写
例:String s = "ABC";
System.out.println(s.toLowerCase());
执行结果:abc
7、toUpperCase() 转换为大写
8、length() 取得字符串的长度
例:char chars[]={'a','b','c','d'};
String s=new String(chars);
int len=s.length();
System.out.println(len);
执行结果:4
9、charAt() 截取一个字符
例:String s = "abcdef.";
System.out.println(s.charAt(2));
执行结果:c
10、getBytes() 替代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()
例:String s = "Hello!你好!";
byte[] bytes = s.getBytes();
System.out.println(bytes);
11、equals()和== equals()方法比较字符串对象中的字符 ==运算符比较两个对象是否引用同一实例。
String s1="Hello";
String s2=new String(s1);
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
执行结果:false
true
12、indexOf() 查找字符或者子串第一次出现的地方
例:String s1="abcdegabc";
System.out.println(s1.indexOf("g"));
执行结果:5
13、split字符串分割
String y = "abc,de,fg,hi,jk";
String[] y1 = y.split(",");// 截取字符串所有","字符
System.out.println(y1.length);
for (int i = 0; i < y1.length; i++) {
System.out.println(y1[i]);
}
执行结果:5
abc
de
fg
hi
jk
14、delete()和deleteCharAt() 删除字符
String y = "abcd,efgh";
StringBuilder sb = new StringBuilder(y);
1:System.out.println(sb.delete(2, 4));//从第startIndex开始删除、至endIndex结束。endIndex不删除
2:System.out.println(sb.length());//字符串的长度
3:System.out.println(sb.deleteCharAt(4));//删除指定的字符串。从0开始
4:System.out.println(sb.deleteCharAt(sb.length()-1));//删除指定字符串的最后一个字符
执行结果:1:ab,efgh
2:9
3:abcdefgh
public static void main(String[] args) {
String str= "select * from table";
String rep="select skip 0 first 3";
System.out.println(str.replaceFirst("select", rep));
}
执行结果:select skip 0 first 3 * from table
public static void main(String[] args) {
String str = "select * from gccasecheck where 1=1 order by regTime desc";
int startNo = str.indexOf("from");
int endNo = str.indexOf("order by");
System.out.println(str.substring(startNo, endNo));
}
public static void main(String[] args) {
String str = "select * from gccasecheck where 1=1 order by regTime desc";
int startNo = str.indexOf("from");
int endNo = str.lastIndexOf("order");
System.out.println(str.substring(startNo, endNo));
}
执行结果:from gccasecheck where 1=1