String 类详解

public final class String

继承自java.lang.Object类。

实现了接口:

java.io.Serializable, Comparable<String>, CharSequence

String类是不能被修改的字符串。因为String类里面的数据是final类型的。

-------------------------注意事项-----------------------

String类的equals()方法判断是的字符串内容。

String是常量,它的方法是返回新的String类型。

String Pool:字符串池。

区别两种方式:

① String s = "aaaa";

② String s = new String("aaaa");

对于第一种,只在池中创建,然后返回对象地址。之后所有“aaaa"都是这个地址。

但是第二种,在池中创建,还要在堆(heap)中创建一个对象,返回的是堆中的地址。因为new了嘛。

 

-------------------------构造函数------------------------

String()

String(String original)

String(char[] value)

String(char[] vlaue, int offset, int count)

String(int[] codePoints, int offset, int count)

String(StringBuffer buffer)

String(StringBuilder builder)

------------------------常用方法---------------------------

public int length(): 返回count,字符个数。

public boolean isEmpty(): 返回bolean值。

public char charAt(int index): 返回index下标位置的字符。

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin): 复制字符串(从srcBegin到srcEnd-1)到指定字符数组(dst)中。从det中下标detBegin开始。

public byte[] getBytes(String charsetName): 转换为byte数组

public boolean equals(Object anObject): 比较值。

public boolean contentEquals(StringBuffer sb): 与stringBuffer比较。

public boolean equalsIgnoreCase(String anotherString): 忽略大小写进行比较

public int compareTo(String anotherString): 比较字符串大小,相等返回0, 大于返回大于0的数,小于返回小于0的数。

public int compare(String s1, String s2): 比较两个字符串大小。

public boolean regionMatches(int toffset, String other, int ooffset,
int len): 比较两个字符串从offset处的len个字符是否相等。

public boolean startsWith(String prefix, int toffset): 测试字符串是否已指定前缀开始

public boolean endsWith(String suffix): 测试字符串是否已制定后缀结尾。

public int indexOf(int ch): 返回指定字符在此字符串中的位置。

public String substring(int beginIndex, int endIndex): 返回子串

public String concat(String str): 连接两个字符串

public String replace(char oldChar, char newChar): 替换字符

public boolean matches(String regex): 是否匹配给定正则表达式

public boolean contains(CharSequence s): 当且仅当此字符串包含指定的 char 值序列时,返回 true。

public String[] split(String regex, int limit): 根据匹配给定的正则表达式来拆分此字符串。 

public String toLowerCase(): 全部转换为小写

public String toUpperCase(): 全部转换为大写

public String trim(): 返回字符串的副本,忽略前导空白和尾部空白。

public String toString(): 转换为字符串

public char[] toCharArray(): 转换为数组

 

posted on 2012-05-24 14:52  leohxj  阅读(1143)  评论(0编辑  收藏  举报

导航