天天看点

常用API------String、StringBuffer、StringBuilder

String

//字符串长度
public int length() 

//获取index所在位置的字符
public char charAt(int index)

//比较两个字符串的内容-区分大小写
public boolean equals(Object anObject) 

//一般都是系统调用 - 比较两个字符串内容
public int compareTo(String anotherString)

 //s在当前字符串中的位置(从左向右查找) 
public int indexOf(String s)

//从当前字符串中startPoint的位置从左向右查找s在当前字符串位置
public int indexOf(String s ,int startpoint)

//s在当前字符串中的位置(从右向左查找)
public int lastIndexOf(String s) 

//从当前字符串中startPoint的位置从右向左查找s在当前字符串位置
public int lastIndexOf(String s ,int startpoint)

//当前字符串是否以 prefix开头
public boolean startsWith(String prefix)

//当前字符串是否以 suffix结尾
public boolean endsWith(String suffix)

public boolean regionMatches(int firstStart,String other,int otherStart ,int length)
		s和s2作比较 :s2这个字符串是否被s这个字符串所包含
		s = 当前字符串从firstStart位置到最后的位置
		s2 = other这个字符串从otherStart开始偏移量为length截取这个串
		
//将当前字符串从startpint一直到最后全部截取成一个新字符串
public String substring(int startpoint)
		
//从当前字符串的start位置截取到end的位置(左闭右开-包头不包尾)
public String substring(int start,int end)
		
//将当前字符串中的oldChar替换成newChar (所有的oldChar)
pubic String replace(char oldChar,char newChar)
		
//将当前字符串中的old替换成new
public String replaceAll(String old,String new)
		
//去除字符串两端的空格
public String trim()
		
//字符串拼接,将当前字符串和str进行拼接
public String concat(String str)
		
//当前字符串是否包含s
public boolean contains(CharSequence s)
		
//将当前字符串以regex的格式进行切割-返回一个字符串数组
public String[] split(String regex)

//将小写转成大写
System.out.println("abc".toUpperCase()); 

 //将大写转成小写
System.out.println("AbC".toLowerCase()); 

//字符串内容进行比较忽略大小写
System.out.println("aaa".equalsIgnoreCase("AAA")); 

//将字符串转成char数组
char[] charArray = "abcdefg".toCharArray();

//将char[]转成字符串
String s = new String(charArray);
System.out.println(s);

//从charArray数组中的索引值为2向后偏移3个位置转成新的字符串
System.out.println(new String(charArray, 2, 3));//3  偏移量

//将字符串转成byte类型的数组
byte[] bytes = "abcdefg".getBytes();

//将byte[]转成字符串
String s2 = new String(bytes);
           

StringBuffer和StringBuilder

//添加数据
StringBuffer append(String s),   StringBuffer append(int n) ,  
StringBuffer append(Object o) ,  StringBuffer append(char n),
StringBuffer append(long n),  StringBuffer append(boolean n),

//插入:在当前字符串的index位置插入str
StringBuffer insert(int index, String str) 

//反转
public StringBuffer reverse() 

//将当前字符串中startIndex到endIndex的位置上的元素删除掉。包头不包尾
StringBuffer delete(int startIndex, int endIndex) 

//获取当前字符串n位置上的字符
public char charAt(int n )

//将当前字符串n位置上的元素替换成ch
public void setCharAt(int n ,char ch)

//将当前字符串startIndex到endIndex位置上的元素替换成str(左闭右开)
StringBuffer replace( int startIndex ,int endIndex, String str) 

//str的当前字符串中出现的位置-从左到右
public int indexOf(String str)

//截取子串,从当前字符串的start到end位置上的元素(包头不包尾)
public String substring(int start,int end)

//当前字符串的长度
public int length()