String与StringBuffer常用方法总结

前言

String与StringBuffer的区别,简单地说,就是一个变量和常量的关系。StringBuffer对象的内容可以修改;而String对象一旦产生后就不可以被修改,重新赋值其实是两个对象。

String常用方法

在之前的数据结构与算法中已经模拟了部分String常用的方法

public final class Stringextends Objectimplements Serializable, Comparable, CharSequence

构造方法

public String():初始化一个新创建的 String 对象,使其表示一个空字符序列。注意,由于 String 是不可变的,所以无需使用此构造方法。
public String(String original):创建一个String对象为original的拷贝
public String(char[] value):用一个字符数组创建一个String对象
public String(char[] value, int offset,int count):用一个字符数组从offset项开始的count个字符序列创建一个String对象

普通方法

public char charAt(int index):返回字符串中第index个字符
public int length():返回字符串的长度
public int indexOf(String str):返回字符串中出现str的第一个位置
public int indexOf(String str,int fromIndex):返回字符串中从fromIndex开始起出现str的第一个位置
public boolean equalsIgnoreCase(String another):比较字符串与another是否一样(忽略大小写)
public String replace(char oldChar,char newChar):在字符串中用newChar替换oldChar
public boolean startWith(String prefix):判断字符串是否以prefix字符串开头
public boolean endsWith(String suffix):判断字符串是否以suffix为结尾
public String toLowerCase():返回一个字符串为该字符串的小写形式
public String substring(int beginIndex):返回该字符串从beginIndex开始到结尾的字符串
public String substring(int beginIndex,int endIndex):返回该字符串从beginIndex开始到endIndex结尾的字符串
public String trim():返回将该字符串去掉开头和结尾空格后的字符串

静态重载方法

public static String valueOf(..):将基本类型数据转化为字符串,如
public static String valueOf(double d)

其它常用方法

public String[] split(String regix):可以将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组
public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于”+”;
contains(String str)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。
字符串转换为基本类型
java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:
1)public static byte parseByte(String s)
2)public static short parseShort(String s)
3)public static short parseInt(String s)
4)public static long parseLong(String s)
5)public static float parseFloat(String s)
6)public static double parseDouble(String s)

字符串比较

1)public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
2)public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。
3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。

StringBuffer常用方法

构造方法

StringBuffer() :构造一个没有任何字符的StringBuffer类。
StringBuffer(int length) ::构造一个没有任何字符的StringBuffer类,并且,其长度为length。
StringBuffer(String str) :以str为初始值构造一个StringBuffer类。

append方法
为该对象添加字符序列,返回添加后的该对象引用,同时是调用了toString方法转化为字符串

  1. StringBuffer append(boolean b)
  2. StringBuffer append(char c)
  3. StringBuffer append(char[] str)
  4. StringBuffer append(char[] str, int offset, int len)
  5. StringBuffer append(double d)
  6. StringBuffer append(float f)
  7. StringBuffer append(int i)
  8. StringBuffer append(long l)
  9. StringBuffer append(Object obj)
  10. StringBuffer append(String str)
  11. StringBuffer append(StringBuffer sb)

其他方法

12.int capacity() :返回当前StringBuffer对象(字符串缓冲区)的总空间,而非字符号串的长度。

  1. char charAt(int index) :在当前StringBuffer对象中取索引号为index的字符。第一个字符的索引为“0”
  2. StringBuffer delete(int start, int end) :删除当前StringBuffer对象中以索引号start开始,到end结束的子串。
  3. StringBuffer deleteCharAt(int index) :删除当前StringBuffer对象中索引号为index的字符。
  4. void ensureCapacity(int minimumCapacity) :重新设置字符号串缓冲区的总空间。如果minimumCapacity大于当前的总空间,则新的空间被设置:一种结果是minimumCapacity;另一种结果是{“老空间”乘2加2}
    17.void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) :从当前StringBuffer对象的索引号srcBegin开始,到srcEnd结束的子串,赋值到字符数组dst中,并且从dst的索引号dstBegin开始
  5. int indexOf(String str) :返回当前StringBuffer对象中,第一个满足str子串的位置。
  6. int indexOf(String str, int fromIndex) :从当前StringBuffer对象的fromIndex开始查找,返回第一个满足str子串的位置。
  7. StringBuffer insert(int offset, boolean b)
  8. StringBuffer insert(int offset, char c)
  9. StringBuffer insert(int offset, char[] str)
  10. StringBuffer insert(int index, char[] str, int offset, int len)
  11. StringBuffer insert(int offset, double d)
  12. StringBuffer insert(int offset, float f)
  13. StringBuffer insert(int offset, int i)
  14. StringBuffer insert(int offset, long l)
  15. StringBuffer insert(int offset, Object obj)
  16. StringBuffer insert(int offset, String str)
  17. int lastIndexOf(String str) :返回当前StringBuffer对象中,最后一个满足str子串的位置。
  18. int lastIndexOf(String str, int fromIndex) :从当前StringBuffer对象的fromIndex开始查找,返回最后一个满足str子串的位置。
  19. int length() :返回当前StringBuffer对象(字符缓冲区)中,字符串的长度。注意:此方法与capacity() 不同。
  20. StringBuffer replace(int start, int end, String str) :替换当前StringBuffer对象的字符串。从start开始,到end结束的位置替换成str。
  21. StringBuffer reverse() :将字符串翻转。
  22. void setCharAt(int index, char ch) :设置索引号index的字符为ch。
  23. void setLength(int newLength) :重新设置字符串缓冲区中字符串的长度,如果newLength小于当前的字符串长度,将截去多余的字符。
  24. String substring(int start) :取当前StringBuffer对象中,从start开始到结尾的子串。
  25. String substring(int start, int end) :取当前StringBuffer对象中,从start开始到end的子串。
  26. String toString() :将当前StringBuffer对象转换成String对象。

热评文章