第六章 常用類總結


 

 

字符串相關類(string stringbuffer

基本數據類型包裝類

Math

File

枚舉類

 

 

 string

Java.lang.string 類代表   不可變的字符序列。

xxxxxxx”爲該類的一個對象

String類的常見構造方法:

String(String  original)

    創建一個String 對象爲original 的拷貝

String char[] value

    用一個字符數組創建一個string的 對象

String (char[] value.int offset.int count)

     用一個字符數組從offset項開始的count個字符序列 創建一個string對象

(再次複習)

Java.lang.string 類代表 不可變的 字符序列

xxxxxxx”爲該類的一個對象。

String類常見的構造方法

       String(string(original))

            創建一個string 對象爲original 的拷貝

      String(char[] value)

             用一個字符組創建一個string對象

      Stringchar[]  value,int offset,int count

            用一個字符組從offset項開始的count個字符序列創建一個string序列

//複習結束

 

      

類舉例代碼如下

Public Class Test{

   Public static void main(string[] args){

      String s1=”hello”;string s2=”world”;

      String s3=”hello”;

      System.out.println(s1==s3);//true

      

      S1=new string(“hello”);

      S2=new sreing(‘hello”);

      System.out.println(s1==s2);//false

      System.out.println(s1.equals(s2));//true

  

      Char c[]={‘s’,’u’,’n’,’j’,’a’,’v’,’a’};

      String s4=new string(c);

      String s5=new string(c,4,4);

      System.out.println(s4);//sun java

      System.out.println(s5);//java

}

}

      

(複習一遍)

Public class test

  Public static void mainstring[] args{

      String  s1=”hello”;string  s2=”world”;

      String  s3=”hello”;

      System.out.println(s1=s3);//true

   

       S1=new string(“hello”);

       S2=new string(“hello”);

       System.out.println(s1==s2);//false

       System.out.println(s1.equals(s2));//true

     

       Char c[]={‘s’,’u’,’n’,’j’,’a’,’v’,’a’};

       String  s4=new string(c);

       String  s5=new string(c,4,4);

       System.out.println(s4);//sun java

       System.out.println(s5);//java

 

(複習完畢)

 

 

 

 

 

  public 類常用方法(1

 

Public char charAt(int index)

返回字符串第index個字符

Public int length()

返回字符串的長度

Public int indexofstring str

返回字符串中出現str的第一個位置

Public int indexofstring strintfromindex

返回字符串中從fromindex 開始出現str的 第一個位置

(複習一遍)

   Public  char  charAt(int index)

   返回字符串中 index個字符;

Public int length()

返回字符串的 長度、;

Public int indexof(string str)

返回字符串 出現str的 第一個位置

Public intindexof(string str,int fromindex)

返回字符串中從fromindex開始出現str的 第一個位置

(繼續開始)

public boolean  equalsignorecase(string another)

比較字符串和another是否一樣 (忽略大小寫)

Public string replace(char oldchar,char newChar)

在字符串中 newchar字符替換oldchar字符。

(再次全部複習一遍)

Public char charAt(int index)

 

返回字符串中第index個字符

 

Public int length()

返回字符串的 長度

Public int indexof(string str)

Public int indexof9string str)

返回字符串出現str 的第一個位置

Public  int  indexof (string str,int fromindex)

Public  int  indexof(string str,int fromindex)

返回字符串 fromindex開始到出現str的 第一個位置)

Public Boolean equalsignorecase(string another)

Public boolean equalsignorecase(string another)

Public Boolean equalsignorecase(string another)

比較和字符串another 是否完全一樣(忽略大小寫)

Public string replace(char oldchar,char newchar)

Public string replace(char oldchar,char newchar)

在字符串裏面用newchar 字符代替oldchar字符

  

 

 

 

String類舉例(2)

 

Public class test{

  Public static void main(string[] args){

      System.out.println(s1.charAt(1));//u

      System.out.println(s2.length());//8

                    System.out.println(s1.indexof(”java”));//4

System.out.println(s1.indexof(“java”));//-1

System.out.println(s1.equals(s2));//false

System.out.println(s1.equalsignorecase(s2));

//true

String s=”我是程序員,我在學Java”;

String sr=s.replace(‘ ‘,’你’);

System.out.println(sr);

//你是程序員,你在學Java

}

}

 

(複習一遍)

Public class test

  Public static void main(string[] args){

String s1=”sun java”,s2=”Sun Java”;

System.out.println(s1.charAt(1));//u

System.out.println(s2.length());//8

System.out.println(s1.indexof(“java”));//4

(再來一遍)

Public class test

  Public static void main(string[] args){

    String s1=“sun java”,s2=”Sun Java”;

    System.out.println(s1.charAt(1));//u

//爲什麼是 u  ,有點疑問

    System.out.println(s1.indexof(“java”));//4

//wwhy      the  second  question.

 

System.out.println(s1.indexof(“Java”));//-1

System.out.println(s1.equals(s2));//false

System.out.println(s1.equalsignorecase(s2));//true

 

String s=”我是程序員,我在學java”;

String sr=s.replace(‘我’,‘你’);

//sr 是什麼意思?第三個問題

System.out.println(sr);

//你是程序員,你在學java

String 類常用方法(二)

Public Boolean startwith(string prefix)

判斷字符串是否以 prefix開頭

Public Boolean startwith(string prefix)

判斷 字符串是否以 prefix開頭

Public Boolean endwith(string  suffix)

------------------suffix結尾

Public string touppercase()

返回一個字符串爲該字符串的大寫形式

Public string touppercase()

返回一個字符串爲該字符串的大寫形式

Public string tolowercase();

返回一個字符串爲該字符串的小寫形式

Public string substring(int beginindex)

Public string substring(int beginindex)

返回該字符串從beginindex開始到結尾的子字符串

Public string substring(int beginindex,int endindex)

Public string substring(int beginindex,int

Endindex)

返回到該字符串從beginindex到endindex結尾的字符串

Public string trim()

Public string stim()

 返回到該字符串去掉開頭和結尾空格後的 字符串

返回到該字符串去掉開頭和結尾空格的 字符串

(review   一遍)

Public Boolean startwith(string prefix)

Public boolean startwith(string prefix)

判斷 字符串是否以 prefix字符串開頭

Public boolean endwith(string suffix)

判斷字符串是否以prefix 字符結尾

Public string touppercase()

返回到一個字符串爲該字符串的大寫形式

Public string tolowercase()

返回 該字符串 爲小寫形式

Public string substring(int beginindex)

返回該字符串 beginindex開始到結尾的子字符串

 

 

 

 

實例代碼

Public class  test{

  Public  static void main (string [] args){

      String s=”welcome to java  world

 

 

 

 

 

 

String類常用方法(3)

靜態重載方法

  Public static string valueof()   可以將基本語句裝換成 字符串,例如

Public static string valueof(double d)

Public  statyic string valueof(double d)

Public   static stringof(int i)

 

方法public string(string regex)可以將一個字符串按照指定的分割符分割 ,返回分隔後的字符串數組。

 

String類舉例(4)

package test;

 

public class teststringclass04 {

  public static  void main(String[] args){

  int j=1234567;

  String sNumber=String.valueOf(j);

  System.out.println

  ("j是"+sNumber.length()+"位數。");

  String s="Mary,F,1976";

  String[] sPlit=s.split(",");

  for(int i=0;i<sPlit.length;i++){

   System.out.println(sPlit[i]);

  }

  }

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章