java.lang.String类常用方法

java.lang.String类
  1. public final class String extends Object implements Serializable,Comparable<String>,CharSequence
String类代表字符串
字符串是常量,他们的值在创建之后不能改变
String类包括的方法有:检查序列的单个字符;比较字符串;搜索字符串;提取子字符串;创建字符串副本(在该副本中,所有的字符都被转换为大写或小写形式)。
Java语言提供对字符串串联符号(“+”)和其他对象到字符串的转换的特殊支持。
字符串串联是通过StringBuilder(或StringBuffer)类及其append方法实现的。
字符串转换是通过toString方法实现的,该方法有Object类定义,并可被Java中所有类继承
方法:
s.chartAt(int index)
返回指定索引处的char值。索引范围:0-length()-1.
抛出:IndexOutBoundsException(如果index参数为负数或小于此字符串的长度)
  1. public static void main(String[] args) {
  2. String s = "wozhishishishizhegeyonfadaodihaobuhaoyong";
  3. System.out.println(s.charAt(2));
  4. }
  5. //运行结果:
  6. z
s.compareTo(String anotherString)
按字典顺序比较两个字符串。该比较基于字符串中各个字符的Unicode值。
将此String对象表示的字符序列与参数字符串所表示的字符序列进行比较。如果按照字典顺序此String对象在参数字符串之前,则结果为一个负整数,在参数字符串之后,则比较结果为一个正整数。如果两个字符串相等,则结果为0.compareTo只有在方法equals(Object)返回true时才会返回0.
如果这两个字符串不同,要么他们在某个索引处有不同的字符,该索引对二者均为有效索引,要么他们的长度不同,或者同时具备上述两者情况。
如果它们在一个或多个索引位置上具有不同的字符,假设k是这类索引的最小值,compareTo返回这两个字符串在位置K处的两个不同的char值:this.charAt(k)-anotherString.charAt(k)。如果它们没有不同的索引位置,则较短字符串在字典顺序上位于较长字符串的前面。这种情况下,compareTo返回这两个字符串长度的不同:this.length()-anotherString.length()
s.compareToIgnoreCase(String str)
不考虑大小写,安字段顺序比较两个字符串
s.concat(String str)
将指定字符串连到字符串的结尾
如果参数字符串的长度为0,则返回此String对象,否则,创建一个新的String对象。用来表示由此String对象表示的字符序列和有参数字符串表示的字符序列串联而成的字符序列
  1. public static void main(String[] args) {
  2. String s = "wozhi";
  3. System.out.println(s.concat("nihaoya"));
  4. }
  5. 运行结果:
  6. wozhinihaoya
s.contains(CharSequence ss)
当且仅当此字符串包含char值的指定序列时,才返回true.
抛出:NullPointerException(如果ss为null)
  1. public static void main(String[] args) {
  2. String s = "wozhi";
  3. System.out.println(s.contains("o"));
  4. }
  5. 运行结果:
  6. true
s.contentEquals(CharSequence cs)
当且仅当此String表示与指定序列相同的char值的序列时;才返回true,否则返回false.
抛出:NullPointerException(如果cs为null)
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. System.out.println(s.contentEquals("abc"));
  4. System.out.println(s.contentEquals("efg"));
  5. System.out.println(s.contentEquals("abcdefg"));
  6. }
  7. 运行结果:
  8. false
  9. false
  10. true
 
copyValueOf(char[] data)
返回指定数组中表示该字符序列的字符串
copyValueOf(char[] data,int index,int count)
返回指定数组中表示该字符序列的字符串(index表示从第几个字符开始,count表示总长度为多少)
  1. public static void main(String[] args) {
  2. char[] charArr = {'j','a','v','a'};
  3. String s = String.copyValueOf(charArr);
  4. String ss = String.copyValueOf(charArr,2,2);
  5. System.out.println(s);
  6. System.out.println(ss);
  7. }
  8. 运行结果:
  9. java
  10. va
endsWith(String suffix)
测试此字符串是否以指定的后缀结束
如果该参数是空字符串或等于有equals(object)方法确定的String对象,则结果为true
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. System.out.println(s.endsWith(""));
  4. System.out.println(s.endsWith("fg"));
  5. System.out.println(s.endsWith("abcdefg"));
  6. System.out.println(s.endsWith("g"));
  7. }
  8. 运行结果:
  9. true
  10. true
  11. true
  12. true
equals(String anObject)
比较此字符串与指定对象。当且仅当该参数不为null,并且是表示与此对象相同的字符序列的String对象,结果才为true
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. String ss = "";
  4. System.out.println(s.equals("abcdefg"));
  5. System.out.println(ss.equals(""));
  6. }
  7. 运行结果:
  8. true
  9. true
equalsIgnoreCase(String anotherString)
将两个String进行比较(字符串长度相等,且相应的字符都相等),不考虑大小写。
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. String ss = "ABCDEFG";
  4. System.out.println(s.equalsIgnoreCase(ss));
  5. }
  6. 运行结果:
  7. true
hashCode()
返回此字符串的哈希码。String对象的哈希码按下列公式计算:s[0]*31^(n-1)+s[1]*31^(n-2)+……+s[n-1].空字符串的哈希码为0
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. System.out.println(s.hashCode());
  4. }
  5. 运行结果:
  6. -1206291356
indexOf(int ch)
返回指定字符在此字符串中第一次出现的索引,若未出现此字符,则返回-1
indexOf(int ch,int fromIndex)
从指定的索引开始搜索,返回在此字符串中第一次出现指定字符处的索引
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. System.out.println(s.indexOf("a"));
  4. System.out.println(s.indexOf("cde"));
  5. System.out.println(s.indexOf("o"));
  6. }
  7. 运行结果:
  8. 0
  9. 2
  10. -1
  1. public static void main(String[] args) {
  2. String s = "abcadefg";
  3. System.out.println(s.indexOf("a",2));
  4. }
  5. 运行结果:
  6. 3
lastIndexOf(String str,int formIndex)
从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引
lastIndexOf(String str)
饭后在此字符串中最右边出现的指定子字符串的索引
  1. public static void main(String[] args) {
  2. String s = "abcadefg";
  3. System.out.println(s.lastIndexOf("a"));
  4. System.out.println(s.lastIndexOf("a",2));
  5. }
  6. 运行结果:
  7. 3
  8. 0
length()
返回此字符串的长度
matches(String regex)
通知此字符串食肉匹配给定的正则表达式
replace(char oldChar,char newChar)
返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar而生成的
replace(CharSequence target,CharSequence replacement)
使用指定的字面值替换序列替换此字符串匹配字面值目标序列的每个子字符串
replaceAll(String regex,String replacement)
使用指定的字面值替换此字符串匹配字面值目标序列的每个字子字符串
replaceFirst(String regex,String replacement)
使用给定的replacement字符串替换此字符串匹配给定的正则表达式的第一个子字符串
split(String regex)
根据匹配给定的正则表达式来拆分此字符串
startWith(String prefix)
测试此字符串是否以指定的前缀开始
substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。该子字符串始于指定索引处的字符,一直到此字符串末尾
substring(int beginIndex.int endIndex)
返回一个新的字符串,始于指定索引beginIndex,终于指定索引endIndex的字符
  1. public static void main(String[] args) {
  2. String s = "abcadefg";
  3. System.out.println(s.substring(2));
  4. System.out.println(s.substring(2,5));
  5. }
  6. 运行结果:
  7. cadefg
  8. cad
toCharArray()
将此字符串转换为一个新的字符数组
  1. public static void main(String[] args) {
  2. String s = "abcadefg";
  3. char[] ss = s.toCharArray();
  4. System.out.println(ss);
  5. }
  6. 运行结果:
  7. abcadefg
toLowerCase()
使用默认语言环境的规则将此String中的所有字符都转换成小写
toUpperCase()
使用默认语言环境的规则将此String中的所有字符都转换为大写
  1. public static void main(String[] args) {
  2. String s = "abcadefg";
  3. String ss = "ABCDEFG";
  4. System.out.println(s.toUpperCase());
  5. System.out.println(ss.toLowerCase());
  6. }
  7. 运行结果:
  8. ABCADEFG
  9. abcdefg
valueOf(boolean b)
返回Boolean参数额字符串表示形式
valueOf(char c)
返回char参数的字符串表示形式
valueOf(int i)
返回int参数的字符串表现形式;long、float、double同样
  1. public static void main(String[] args) {
  2. char s = 'q';
  3. System.out.println(String.valueOf(false));
  4. System.out.println(String.valueOf(s));
  5. System.out.println(String.valueOf(2.34));
  6. System.out.println(String.valueOf(123456));
  7. System.out.println(String.valueOf(1));
  8. }
  9. 运行结果:
  10. false
  11. q
  12. 2.34
  13. 123456
  14. 1
原文地址:https://www.cnblogs.com/jodiegreat/p/6673688.html