java編程筆記5 主要是StringBuffer和StringTokenizer

來源:互聯網
上載者:User
2011-03-02 20:08

一,    字串數組 

建構函式: 

String[]  array = new String[5];

String[] array =new String[] {“aa”,”bb”,”cc”};

String[] array = {“aa”,”bb”,”cc”};

字串數組也被看做對象: 

屬性:length (區別於字串的方法length())

常用方法:void System.arraycopy(object[] from,intfromIndex, object[] to,int fromIndex,int count),系統的一個靜態方法

二,字串緩衝類StringBuffer

String類對象一旦聲明就不能改變,StringBuffer緩衝類實現了可變字元序列。

StringBuffer上的基本操作是append和insert方法,它們接受任意的資料,把所給資料轉化為一個字串,並把這個字串添加到緩衝區的末端或者插到指定地方。

public final class StringBuffer

extends Object

implements Serializable, CharSequence

構造方法摘要

StringBuffer()
          構造一個其中不帶字元的字串緩衝區,初始容量為 16 個字元。

StringBuffer(CharSequence seq)
          public java.lang.StringBuilder(CharSequence seq) 構造一個字串緩衝區,它包含與指定的  CharSequence 相同的字元。

StringBuffer(int capacity)
          構造一個不帶字元,但具有指定初始容量的字串緩衝區。

StringBuffer(String str)
          構造一個字串緩衝區,並將其內容初始化為指定的字串內容。

其他方法:

StringBuffer轉化為String對象:toString()

 String

toString()
          Returns a string representing the data in this sequence.

StringBuffer的擴充appand():

StringBuffer

append(boolean b)
          Appends the string representation of the boolean argument to the sequence.

 StringBuffer

append(char c)
          Appends the string representation of the char argument to this sequence.

 StringBuffer

append(char[] str)
          Appends the string representation of the char array argument to this sequence.

 StringBuffer

append(char[] str, int offset, int len)
          Appends the string representation of a subarray of the char array argument to this sequence.

 StringBuffer

append(CharSequence s)
          將指定的 CharSequence 添加到該序列。

 StringBuffer

append(CharSequence s, int start, int end)
          Appends a subsequence of the specified CharSequence to this sequence.

等等…….

任意類型都將被轉化為字串加入緩衝末端

StringBuffer的插入insert():

StringBuffer

insert(int offset, Object obj)
          Inserts the string representation of the Object argument into this character sequence.

 StringBuffer

insert(int offset, String str)
          Inserts the string into this character sequence.

StringBuffer

insert(int offset, char[] str)
          Inserts the string representation of the char array argument into this sequence.

 StringBuffer

insert(int index, char[] str, int offset, int len)
          Inserts the string representation of a subarray of the str array argument into this sequence.

等等……

典型的其他動作:

 int

length()
          Returns the length (character count).

 char

charAt(int index)
          Returns the char value in this sequence at the specified index.

 String

substring(int start)
          Returns a new String that contains a subsequence of characters currently contained in this character sequence.

 int

indexOf(String str)
          Returns the index within this string of the first occurrence of the specified substring.

StringBuffer

replace(int start, int end, String str)
          Replaces the characters in a substring of this sequence with characters in the specified String.

 StringBuffer

delete(int start, int end)
          Removes the characters in a substring of this sequence.

等等…..

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.