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

java编程笔记5 主要是StringBuffer和StringTokenizer

2013年06月30日 ⁄ 综合 ⁄ 共 2770字 ⁄ 字号 评论关闭
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上的基本操作是appendinsert方法,它们接受任意的数据,把所给数据转化为一个字符串,并把这个字符串添加到缓冲区的末端或者插到指定地方。

public final class StringBuffer

extends Object

implements SerializableCharSequence

构造方法摘要

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.

等等…..

抱歉!评论已关闭.