Java中的字符串也是一连串的字符,但是Java将字符串作为String类型对象来处理

1、String构造函数

  • 创建空String的构造函数称为默认构造函数

String s = new String();

  • 用如下的构造函数可以创建一个被字符数组初始化的字符串

String s = new String(char chars[]);

  • 使用下面的构造函数可以指定字符数组的一个子区域作为初始化值

String s = new String(char chars[], int startIndex, int numChars);

  •  
  • 用下面的构造函数可以构造一个String对象,该对象包括了与另一个String对象相同的字符序列。

String s = new String(String strObj);


2、字符串长度

int length()


3、特殊的字符串操作

1)字符串文字

String s = "abc";

2)字符串连接

可以使用运算符+连接字符串

3)和其他类型的数据连接

自动转为String类型

4)字符串转换和toString()

当Java 在连接时将数据转换为其字符串形式时,它是通过调用一个由字符(String)定义的字符串转换方法valueOf( )的重载来完成的。


4、字符截取

String类提供了许多从String对象中截取字符的方法

1)charAt()

从字符串中截取一个字符

charAt(int where)

  1. public static void main(String[] args) { 
  2.     String s="abcde"
  3.     System.out.println(s.charAt(1)); 
  4. //代码运行输出为b  说明下标从0开始 

 

2)getChars()

如果一次想截取多个字符,可以使用getChars()

  1. void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart) 

 

这里sourceStart 指定了子字符串开始的下标,

sourceEnd指定了子字符串结束的下一个字符的下标。

因此子字符串包含了从sourceStart 到sourceEnd–1的字符。

获得字符的数组由target所指定。

 

  1. public static void main(String[] args) { 
  2.     String s="abcdefg"
  3.     int start = 1
  4.     int end =3
  5.     char buf[] = new char[end - start]; 
  6.     s.getChars(start, end, buf, 0); 
  7.     System.out.println(buf); 
  8. //结果输出为  bc 

3)getBytes()

 

有一种称为getBytes( )的方法,它是实现将字符存放于字节数组中的getChars( )方法的替代,它使用平台提供的默认的字符到字节的转换。

4)tocharArray()

 

如果想将字符串(String)对象中的字符转换为一个字符数组,最简单的方法就是调用toCharArray( ) 方法。对应整个字符串,它返回一个字符数组


5、字符串比较

1)equals()和equalsIgnoreCase()

使用equals( ) 方法比较两个字符串是否相等

  1. boolean equals(Object str)  

 为了执行忽略大小写的比较,可以调用equalsIgnoreCase( ) 方法

  1. boolean equalsIgnoreCase(String str)  

 2)regionMatches()

regionMatches( ) 方法将一个字符串中指定的区间和另一字符串中指定的区间进行比较。它的重载形式允许在比较时忽略大小写。

  1. boolean regionMatches(int startIndex, String str2,  int str2StartIndex, int numChars)
  2. //  ignoreCase是否区分大小写
  3. boolean regionMatches(boolean ignoreCase,int startIndex, String str2,int str2StartIndex, int numChars) 

接下来看下这个方法的使用

  1. public static void main(String[] args) { 
  2.     String s1 = "abcdefg"
  3.     String s2 = "bcdefgh"
  4.     String s3 = "hijklmn"
  5. //s1从下标1开始截取3个字符串    和  s2从下标0开始截取3个字符串  相比较 
  6.     System.out.println(s1.regionMatches(1, s2, 03)); 
  7. //s1从下标1开始截取3个字符串    和  s3从下标1开始截取3个字符串  相比较 
  8.     System.out.println(s1.regionMatches(1, s3, 13)); 
  9. //第一行输出true  第二行输出false 

 3)startsWith()和endsWith()

startsWith( ) 方法判断一个给定的字符串(String)是否从一个指定的字符串开始

endsWith( ) 方法判断所讨论的字符串(String)是否是以一个指定的字符串结尾

  1. boolean startsWith(String str)  
  2. boolean endsWith(String str) 

 例子:

  1. public static void main(String[] args) { 
  2.     String s="abcdefgh"
  3.     System.out.println("s为:  "+s); 
  4.     System.out.println("s是否是ab开头"+s.startsWith("ab")); 
  5.     System.out.println("s是否是gh开头"+s.endsWith("gh")); 
  6.     System.out.println("s是否是bc开头"+s.startsWith("bc")); 
  7.     System.out.println("s是否是hi开头"+s.endsWith("hi")); 
  8. // 输出内容 
  9. // ↓↓↓ 
  10. //     s为:  abcdefgh 
  11. //     s是否是ab开头:  true 
  12. //     s是否是gh开头:  true 
  13. //     s是否是bc开头:  false 
  14. //     s是否是hi开头:  false 

 4) equals()与==的比较 

equals( ) 方法比较字符串(String)对象中的字符

==运算符比较两个对象引用看它们是否引用相同的实例

  1. public static void main(String[] args) { 
  2.         String s1 = "abc"
  3.         String s2 = "abc"
  4.         System.out.println("equals结果:\t"+s1.equals(s2)); 
  5.         System.out.println("==结果:\t"+(s1==s2)); 
  6.     } 
  7. //          输出结果 
  8. //          ↓↓↓ 
  9. //      equals结果:   true 
  10. //      ==结果:   true 

 5)compareTo()

一个字符串小于另一个指的是它在字典中先出现,而一个字符串大于另一个指的是它在字典中后出现。字符串(String)的compareTo( )方法实现了这种功能

  1. 小于0  调用字符串小于str  
  2. 大于0  调用字符串大于str  
  3. 等于0  两个字符串相等  

 例子:

  1. public static void main(String[] args) { 
  2.         String arr[] = { "Now""is""the""time""for""all""good"
  3.                 "men""to""come""to""the""aid""of""their"
  4.                 "country" }; 
  5.  
  6.         for (int i = 0; i < arr.length; i++) { 
  7.             for (int j = i + 1; j < arr.length; j++) { 
  8.                 if(arr[i].compareTo(arr[j])>0){ 
  9.                     String temp = arr[i]; 
  10.                     arr[i]=arr[j]; 
  11.                     arr[j]=temp; 
  12.                 } 
  13.             } 
  14.         } 
  15.          
  16.         for (int i = 0; i < arr.length; i++) { 
  17.             System.out.print(arr[i]+"  "); 
  18.         } 
  19.     } 
  20.  
  21. //输出为:Now  aid  all  come  country  for  good  is  men  of  the  the  their  time  to  to   
  22. //由于Now的N为大写  所以排在最前面 

如果需要忽略大小写 则用compareToIgnoreCase()


6、搜索字符串

String类提供了两个方法,允许在字符串中搜索指定的字符或子字符串: 

方法返回字符或子字符串被发现的位置的下标,当搜索失败时,返回-1。

  • indexOf()  搜索字符或子字符串首次出现。 

  • lastIndexOf()  搜索字符或子字符串的最后一次出现。 

indexOf(int ch)

indexOf(String str)

indexOf(int ch, int fromIndex)

indexOf(String str, int fromIndex)

 

lastIndexOf(int ch)

lastIndexOf(String str)

lastIndexOf(int ch, int fromIndex)

lastIndexOf(String str, int fromIndex)


7、修改字符串

 1)substring()

截取字符串

  1. //substring 截取字符串 
  2. //返回从下标startIndex到字符串结尾的字符串 
  3. String substring(int startIndex)  
  4. //返回从startIndex到endIndex-1的字符串 
  5. String substring(int startIndex, int endIndex) 

2)concat()

连接两个字符串

String concat(String str)

  1. public static void main(String[] args) { 
  2.     String s = "abcdefg"
  3.     String s2 = "hijk"
  4.     System.out.println(s.concat(s2)); 
  5. //  输出内容 
  6. //  ↓↓↓ 
  7. //  abcdefghijk 

3)replace()

replace( ) 方法用另一个字符代替调用字符串中一个字符的所有具体值

String replace(char original, char replacement) 

例子:

  1. public static void main(String[] args) { 
  2.     String s = "akljfadja"
  3.     System.out.println(s.replace('a''b')); 
  4.      
  5. //  输出结果 
  6. //  ↓↓↓ 
  7. //  bkljfbdjb 
  8. //  将字符串中的字符a全部替换为字符b 

4)trim()

 

trim( ) 方法返回一个调用字符串的拷贝,该字符串是将位于调用字符串前面和后面的空白符删除后的剩余部分


 

 8、利用valueOf( )方法实现数据转换

 

  • static String valueOf(double num) 
  • static String valueOf(long num) 
  • static String valueOf(Object ob) 
  • static String valueOf(char chars[])

 

9、改变字符串内字符的大小写

  1. toLowerCase( ) 方法将字符串内的所有字符从大写字母改写为小写字母
  2. toUpperCase( )方法将字符串内所有字符从小写字母改写为大写字母
    例子:
  1. public static void main(String[] args) { 
  2.     String s = "adsGFdasGrSa";  
  3.     System.out.println(s.toLowerCase()); 
  4.     System.out.println(s.toUpperCase()); 
  5.      
  6. //  输出结果 
  7. //  ↓↓↓ 
  8. //  adsgfdasgrsa 
  9. //  ADSGFDASGRSA