String 介绍1
一、String类1
二、String类对象的创建1
三:常用方法2
1、求字符串长度2
2、求字符串某一位置字符2
3、提取子串2
4、字符串比较2
5、字符串连接3
6、字符串中单个字符查找3
(str2);//b=0
5 boolean c = (str2);//c=false
6 boolean d = (str2);//d=true
复制代码
5、字符串连接
public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
1 String str = "aa".concat("bb").concat("cc");
2 相当于String str = "aa"+"bb"+"cc";
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位置向前查找。
复制代码
1 String str = "I am a good student";
2 int a = ('a');//a = 2
3 int b = ("good");//b = 7
4 int c = ("w",2);//c = -1
5 int d = ("a");//d = 5
6 int e = ("a",3);//e = 2
复制代码
7、字符串中字符的大小写转换
1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串
2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串
1 String str = new String("asDF");
2 String str1 = ();//str1 = "asdf"
3 String str2 = ();//str2 = "ASDF"
8、字符串中字符的替换
1)public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
1 String str = "asdzxcasd";
2 String str1 = ('a','g');//str1 = "gsdzxcgsd"
3 String str2 = ("asd","fgh");//str2 = "fghzxcfgh"
4 String str3 = ("asd","fgh");//str3 = "fghzxcasd"
5 String str4 = ("asd","fgh");//str4 = "fghzxcfgh"
9、截去字符串两端的空格
1)String trim()//截去字符串两端的空格,但对于中间的空格不处理。
String常用的方法介绍 来自淘豆网m.daumloan.com转载请标明出处.