background image

  
    第 6 章 Java 库结构与部分语言类
    本章主要讲述 Java

 

类库,字符串类, object 类、system 类等。

    例 6.1 

 

创建串对象 import java.io.*

 

; class CreateString public static void main(String [ ]

args)throws UnsupportedEncodingException { char [ ] chars= {`J`,`a`,`v`,`a`,`程`,`序`,`设`,`
计 `}

 

; byte [ ] bytes= {-61 , -26 , -49 , -14 , -74 , -44 , -49 , -13}

 

; string s1=new 

String(chars

 

); String s2=new String (chars,0,5); String s3=new String(bytes); String s4=new 

String   (bytes,4,4,”GB2312”);   String   s5=new   String(“ok”);   System.out.println(“sl:”+s1+” 
s2:”+s2+” s3:”+s3+” s4:”+s4+” s5:”+s5); } }
    

 

该程序产生的输出是: s1:Java

 

程序设计 s2;Java

 

程 s3

 

:面向对象 s4

 

:对象 s5  

: ok 

public String() 

 

创建空串对象。 public  public String(String value) 

用串对象 value 创建一

个新的串对象,value 

 

可以是字符串或 String 

 

对象。⑶ public String (StringBuffer sb

 

)。用

串缓冲对象 sb

 

创建串对象。⑷ public String (char [ ] value  

) 用字符数组 value 来创建串对

象。
    ⑸ public String (char value[ ],int offest,int count  

) 用字符数组 value 中下标为 offest

的 字 符 开 始 的 count  

 

 

 

个 字 符 创 建 串 对 象 。 ⑹ pulic String  

 

( byte [ ] bytes 

 

, String 

UnsupporedEncodingException enc 

 

) trows

    用字节数组 bytes 按指定的字符编码方案 enc

 

创建串对象。若系 统不支持指定的编码方

案,抛出 CnsupportedEncodingException 

 

异常。 ⑺ public String(bytes [ ]bytes  

) 用字节数

组 bytes

 

按系统缺省的字符编码方案创建串对象。 ⑻ public String(byte[ ] byes,int 

offest,int lengh

 

) 用 bytes 中下标为 offest 的字节开始的 length

 

个字节按系统缺省的 字符

编码方案创建串对象。
    例 6.2 

 

对象的字符串表示 class Pnt{ double x,y; Pnt(double x,double y){this.x=x;this.y=y;} 

public   String   toString(){   return   "( 横 坐 标 :"+x+", 纵 坐 标 :"+y+")";   }   }   class 
ObjectToString{   public   static   void   main(String[]   args){   Pnt   p=new   Pnt(1.2,2.8);   String 
str=String.valueOf(p); System.out.println("\t 对象 p 的字符串表示为:"+str); } }
    ⑴ public String toString() 

 

返回当前字符串。⑵ public static String valueOf(Object obj) 返回

对象 obj 的字符串表示。对象的字符串表示是由 Object

 

的 方法 toString()产生的字符串。

    例 6.5 设计静态方法将串 String newStr 替换主字符串 String primeStr 中的第 1

 

次出现 的

子 串 String oldStr class TestReplaceSubstr { public static String replaceSubstr(String primeStr, 
String newStr, String oldStr){ StringBuffer sb = new SringBuffer(); Int oldStrIndex = primeStr. 
IndexOf(oldStr,0); if(oldStrIndex = = —1){return primeStr} if (oldStrIndex>0){ String frontStr = 
primeStr.substring(0,oldStrIndex); Sb + sb.append(frontStr).append(newStr); }
        else   {   if   (oldStrIndex   =   =   0)   sb=   sb.append(newStr);}   String   rearStr   = 
primeStr.substring(oldStrIndex+   oldStr.length())   return   sb.append(rearStr).toString();   }   public 
static void main (string[] args)throws java.io.IOException { String newPrimeStr = replaceSubstr 
(args[0], args [1], arg [2]) ; System.out.println( "老子串:"+args[2]+ "原主串:"+args[0]+"
新主串:  

" +args[1]+"新主串:"+newPrimesStr) ; } }

    该程序经编译后执行命令行: java TestReplaceSubstr complication bin public 程序产生的输
入是:     

老 子 串: plic combination   

 

原 主串 :complication 

   

新子 串 : bin 

   

新主 串 :

    

 

⑴该方法头的设计: public static String replaceSubstr(String primeStr, String newStr, String 

oldStr) ⑵

 

方法体的算法如下: ①创建 StringBuffer 对象 sb ② 获取 primeStr 中第 1 个 oldStr

子串的首字母下标 oldStrIndex
    ③ if(oldStrIndex==-1)返回 primeStr  if(oldStrIndex>0){ 

将 primeStr 中下标 oldStrIndex 之