现在的位置: 首页 > 综合 > 正文

字符串类

2013年08月21日 ⁄ 综合 ⁄ 共 1857字 ⁄ 字号 评论关闭

1.String类 : String类的对象是已经创建便不能变动内容的字符串常量

Code:
  1. package javase.stu;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.IOException;   
  5. import java.io.InputStreamReader;   
  6.   
  7. public class StringTest {   
  8.   
  9.     public static void main(String[] args) throws IOException {   
  10.            
  11.         char c[] = {'i',' ','l','o','v','e',' ','u'};   
  12.         String s = "i love u";   
  13.         String s1 = new String(c,0,8);   
  14.         System.out.println(s);   
  15.         System.out.println(s1);   
  16.            
  17.         String s2 = s.concat(s1);   
  18.         String s3 = s.replace('i''I');   
  19.         System.out.println(s);   
  20.         System.out.println(s2);   
  21.         System.out.println(s3);   
  22.         System.out.println(s.compareTo(s1));   
  23.         System.out.println(s.compareTo(s3));   
  24.            
  25.         for(int i = 0; i < args.length; i++){   
  26.             System.out.println(args[i]);   
  27.         }   
  28.        
  29.         InputStreamReader input = new InputStreamReader(System.in);   
  30.         BufferedReader read = new BufferedReader(input);   
  31.         System.out.println("input please !");   
  32.         String instr = read.readLine();   
  33.         System.out.println(instr);   
  34.     }   
  35. }   

2.StringBuffer类是一个在操作中可以变更其内容的字符串类,即一旦创建了StringBuffer类的对象,那么在操作中便可以更改和变动字符串的内容

Code:
  1. package javase.stu;   
  2.   
  3. public class StringBufferTest {   
  4.   
  5.     public static void main(String[] args) {   
  6.            
  7.         StringBuffer buff1 = new StringBuffer();   
  8.            
  9.         StringBuffer buff2 = new StringBuffer(8);   
  10.            
  11.         StringBuffer buff3 = new StringBuffer("i love u");   
  12.            
  13.         // length()返回缓冲区中字符串的长度   
  14.         System.out.println(buff3.length());   
  15.            
  16.         // capacity返回缓冲区的长度   
  17.         System.out.println(buff1.capacity());   
  18.            
  19.         buff1.append("i love u");   
  20.         System.out.println(buff1);   
  21.            
  22.         // 插入字符到下标处   
  23.         buff3.insert(7"yo");   
  24.         System.out.println(buff3);   
  25.     }   
  26. }  

 

抱歉!评论已关闭.