4、两个字符串比较
(1)public boolean equals(Object anotherObject)//区分大小写(可以用于密码登录)
(2)public boolean equalsIgnoreCase(String anotherString)//忽略大小写(可以用于登陆验证码)
举例:
String str1 = new String("A1B2");
String str2 = new String("a1b2");
boolean a = str1.equals(str2);//a=false (区分大小写)
boolean b = str1.equalsIgnoreCase(str2);//b=true (不区分大小写)
5、字符串连接
public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果与"+"相同。
举例:
String str = "aa".concat("bb").concat("cc");//aabbcc
6、字符串中单个字符查找
(1)public int indexOf(int ch/String str)//查找当前字符串中字符或子串,当前字符串中从左边起首次出现的位置,没有出现则返回-1。(向后找)
(2)public int indexOf(int ch/String str, int fromIndex)//此方法与第一种相似,区别在于该方法从fromIndex位置向后查找。(向后找)
(3)public int lastIndexOf(int ch/String str)//此方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。(向前找)
(4)public int lastIndexOf(int ch/String str, int fromIndex)//此方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。(向前找)
举例:
String[] str = "fu,ban,piao,liang";
int a = str.indexOf('f');//a = 0 ('f'第一次在0索引出现,从左到右0→17)
int b = str.indexOf("an");//b = 4 ("an"第一次在4索引出现,从左到右0→17)
int c = str.indexOf('f',1);//c = -1('f'没有找到返回-1,从1索引往后找1→17(包括1))
int d = str.lastIndexOf("i");//d = 13('i'第一次在13索引找到,从右到左17→0)
int e = str.lastIndexOf("i",9);//e = 8('i'第一次在索引8找打,从右到左9→0)