String 类常用方法

一、String类

1 String str1 = new String();

2,public String(String value)
用字符串value创建一个String对象

1 String str2 = new String("abcd"); // abcd
2 String str3 = new String(str2); // abcd

3、public String(char[] value)
用字符数组Value创建一个String对象

1 char[] value = {'a','b','c','d'};
2 String str4 = new String(value);//abcd

4,public String (char chars[],int startIndex, int numChars)
用字符数组chars的startIndex开始的numchars个字符创建一个String对象。

1 char[] value = {'a','b','c','d'};
2 String str4 = new String(value,1,2);// bc

5、public String(byte[] values)
用比特数组values创建一个String对象

1 byte[] strb = new byte[]{65,66};
2 String str6 = new String(strb); //AB

四、String类常用方法
1、求字符串长度
public int length() //返回字符串的长度

1 String str = new String("abcd");
2 int len = str.length();   //len = 4;

2、求字符串某一位置字符
public char charAt(int index) //返回字符串中指定位置index的字符,注意字符串中第一个字符索引是0,最后一个是length()-1。

1 String str = new String("abcd");
2 char ch = str.charAt(2);  //ch = c;

3,提取字符串
String类的substring方法可以提取字符串中的子串,该方法有两种参数:
1)public String substring(int beginIndex) //该方法从当前字符串的beginIndex位置起,取出剩余的字符作为一个新的字符串返回。
2)public String substring(int beginIndex,int endIndex) //该方法从当前字符串的beginIndex位置开始,到endIndex-1位置,取出字符作为一个新的字符串返回。

1 String str1 = new String("abcd");
2 String str2 = str1.substring(1);  //bcd
3 String str3 = str1.substring(1,3); //bc

4,字符串比较
1)public int comparTo(String anotherString) //该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0.
2)public int compareToIgnore(String anotherString) //与compareTo方法相同,忽略大小写
3)public boolean equals(Object anotherObject) //比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则false。
4)public boolean equalsIgnoreCase(String anotherString) //与equals方法相似,但忽略大小写。

1 String str1 = new String("abcd");
2 String str2 = new String("ABCD");
3 int a = str1.compareTo(str2); //a=32
4 int b = str1.compareTo(str1); //b=1
5 boolean c = str1.equals(str2); //false
6 boolean d = str1.equalsIgnoreCase(str2); // true

5,字符串连接
public String concat(String str) //将参数中的字符串str连接到当前字符串的后面,效果等同于“+”

String str = "ab".concat("cd").concat("ef");

6,字符串中单个字符查找
1)public int indexOf (int ch/String str) //用于查找当前字符串中字符或子串在当前字符串中的位置,下标0开始,若没有则返回-1.
2)public int indexOf(int ch/String str,int formIndex)//该方法从fromIndex位置开始向后查找字符或子串的位置
3)public int lastIndexOf(int ch/String str) //该方法从字符串末尾位置向前查找
4)public int lastIndexOf(int ch/String str,int fromIndex)

String str = "abcdefabcdef";
int a = str.indexOf('b'); // a = 1
int b = str.indexOf('b',4); //b = 7
int c = str.lastIndexOf('b');//c = 7
int d = str.lastIndexOf('b',4);// d = 1

7,字符串中字符的大小写转换
1)public String toLowerCase() // 将当前字符串所有字符转换成小写
2)public String toUpperCase() //将当前字符串所有字符转换成大写
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相匹配的子串,并将新字符串返回
9,其他方法
1)String trim()//截取字符串两端的空格,字符串中间的空格不处理
2)boolean statWith(String prefix)或boolean endWith(String suffix)// 用来比较当前字符串的起始字符和字符串prefix 或 终止字符串和suffix是否相同
3)contains(String str) //判断参数str是否被包含在字符中,返回布尔值
4)String[] split(String str)// 将str作为分隔符进行字符串分解,返回数组。

猜你喜欢

转载自blog.csdn.net/x279114012/article/details/78729065