String字符串的操作

1、字符串数组与字符串之间的转换

一个字符串可以变为一个字符 数组,同样,也可以把一个字符数组,变为一个字符串。

在String类中提供了以下操作方法:

将字符串变为字符数组:  

  • public char[] toCharArray()
  • public String(char[] value)
  • public String(char[] value,int offset,int count)

[java] view plaincopy
  1. public class StringAPIDemo01{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello" ;         // 定义字符串  
  4.         char c[] = str1.toCharArray() ; // 将一个字符串变为字符数组  
  5.         for(int i=0;i<c.length;i++){ // 循环输出  
  6.             System.out.print(c[i] + "、") ;   
  7.         }  
  8.         System.out.println("") ;        // 换行  
  9.         String str2 = new String(c) ;   // 将全部的字符数组变为String  
  10.         String str3 = new String(c,0,3) ;   // 将部分字符数组变为String  
  11.         System.out.println(str2) ;      // 输出字符串  
  12.         System.out.println(str3) ;      // 输出字符串  
  13.     }  
  14. };  

2、从字符串中取出指定位置的字符

如果要想使用此操作,则肯定此方法的返回值是一个char类型。pbulic charAt(int index)

[java] view plaincopy
  1. public class StringAPIDemo02{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello" ;         // 定义String对象  
  4.         System.out.println(str1.charAt(3)) ;    // 取出字符串中第四个字符  
  5.     }  
  6. };  

3、字符串与byte数组的转换

byte数组(字节数组),在一般的IO操作中会经常使用到。

在String类中提供了以下的方法可以进行字符串与字节数组间的转换:

  • 字符串变为字节数组:public byte[] getBytes()
  • 将一个字节数组变为字符串:  
    • 将全部字节数组变为String:pbulic String(byte[] bytes)
    • 将部分字节数组变为String:public String(byte[] bytes,int offset,int length)

[java] view plaincopy
  1. public class StringAPIDemo03{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello" ;         // 定义字符串  
  4.         byte b[] = str1.getBytes() ;    // 将字符串变为byte数组  
  5.         System.out.println(new String(b)) ; // 将全部的byte数组变为字符串  
  6.         System.out.println(new String(b,1,3)) ; // 将部分的byte数组变为字符串  
  7.     }  
  8. };  

4、取得一个字符串的长度

要想取得字符串的长度:public int length()

[java] view plaincopy
  1. public class StringAPIDemo04{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello LiXingHua" ;       // 定义字符串变量  
  4.         System.out.println("\""+str1+"\"的长度为:"+str1.length()) ;  
  5.     }  
  6. };  

5、查找指定的字符串是否存在

在实际操作中,经常会使用到判断一个字符串中是否存在某些内容,此时就可以用以下的方法:

  • 从头开始查找:public int indexOf(String str)
  • 从指定位置开始查找:public int indexOf(String str,int fromIndex)
查找的时候方法的返回值是一个int类型的数据,此数据表示一个字符串的具体位置,如果没有查找到此字符串,则返回 “-1”。

[java] view plaincopy
  1. public class StringAPIDemo05{  
  2.     public static void main(String args[]){  
  3.         String str1 = "abcdefgcgh" ;                // 声明字符串  
  4.         System.out.println(str1.indexOf("c")) ;     // 查到返回位置  
  5.         System.out.println(str1.indexOf("c",3)) ;   // 查到返回位置,从第4个位置开始查找  
  6.         System.out.println(str1.indexOf("x")) ;     // 没有查到返回-1  
  7.     }  
  8. };  

6、去掉空格

如果假设一些信息是由用户输入的话,则就有可能出现多余的空格,在这种操作中就可以使用trim()

去掉字符串的左右空格,但是字符串中间的空格是不可能去掉的。

[java] view plaincopy
  1. public class StringAPIDemo06{  
  2.     public static void main(String args[]){  
  3.         String str1 = "    hello    " ;     // 定义字符串  
  4.         System.out.println(str1.trim()) ;   // 去掉左右空格后输出  
  5.     }  
  6. };  

7、字符截取

从一个指定的字符串中取出里面的部分内容,使用的方法:

  • 从指定位置开始一直截取到结束位置:public String substring(int beginIndex)
  • 截取指定范围的字符串:public String substring(int beginIndex,int endIndex) 
[java] view plaincopy
  1. public class StringAPIDemo07{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello world" ;       // 定义字符串  
  4.         System.out.println(str1.substring(6)) ; // 从第7个位置开始截取  
  5.         System.out.println(str1.substring(0,5)) ; // 截取0~5个位置的内容  
  6.     }  
  7. };  

8、拆分字符串

如果现在需要按指定的字符串去拆分一个字符串的话,但使用:public String[] split(String regex)

[java] view plaincopy
  1. public class StringAPIDemo08{  
  2.     public static void main(String args[]){  
  3.         String str1 = "hello world" ;       // 定义字符串  
  4.         String s[] = str1.split(" ") ;      // 按空格进行字符串的拆分  
  5.         for(int i=0;i<s.length;i++){     // 循环输出  
  6.             System.out.println(s[i]) ;  
  7.         }  
  8.     }  
  9. };  

9、大小写转换

此功能在一般的开发语言都会存在,将一个大写的字符串全部字母变为小写或者将一个小写的字符串中的全部字母变为大写:

  • 小写变大写:public String toUpperCase()
  • 大写变小写:public String toLowerCase()
[java] view plaincopy
  1. public class StringAPIDemo09{  
  2.     public static void main(String args[]){  
  3.         System.out.println("将\"hello world\"转成大写:" + "hello world".toUpperCase()) ;  
  4.         System.out.println("将\"HELLO WORLD\"转成小写:" + "HELLO WORLD".toLowerCase()) ;  
  5.     }  
  6. };  

10、判断是否以指定的字符串开头或结尾

在String中可以使用以下两种方法完成:

  • 判断是否以指定的字符串开头:public boolean startsWith(String prefix)
  • 判断是否以指定的字符串结尾:public boolean endsWith(String suffix)
[java] view plaincopy
  1. public class StringAPIDemo10{  
  2.     public static void main(String args[]){  
  3.         String str1 = "**HELLO" ;           // 定义字符串  
  4.         String str2 = "HELLO**" ;           // 定义字符串  
  5.         if(str1.startsWith("**")){          // 判断是否以“**”开头  
  6.             System.out.println("(**HELLO)以**开头") ;  
  7.         }  
  8.         if(str2.endsWith("**")){            // 判断是否以“**”结尾  
  9.             System.out.println("(HELLO**)以**结尾") ;  
  10.         }  
  11.     }  
  12. };  

11、不区分大小写的比较

在String类中equals()方法是可以用来进行字符串比较的,但是此种比较方法只能针对于大小写完全一样的字符串进行比较,如果现在要是想进行不区分大小写的比较,则可以使用以下的方法:

  • public boolean equalsGnoreCase(String anotherString)
[java] view plaincopy
  1. public class StringAPIDemo11{  
  2.     public static void main(String args[]){  
  3.         String str1 = "HELLO" ;         // 定义字符串  
  4.         String str2 = "hello" ;         // 定义字符串  
  5.         System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ;  
  6.         System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" "  
  7.                 + str1.equalsIgnoreCase(str2)) ;    // 不区分大小写的比较  
  8.     }  
  9. };  

12、字符串替换功能

在String类中提供了以下的方法用于字符串的替换操作:

  • public String replaceAll(String regex, String replacement)
[java] view plaincopy
  1. public class StringAPIDemo12{  
  2.     public static void main(String args[]){  
  3.         String str = "hello" ;          // 定义字符串  
  4.         String newStr = str.replaceAll("l","x") ;   // 现在将所有的l替换成x  
  5.         System.out.println("替换之后的结果:" + newStr) ;  
  6.     }  
  7. };  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章