一、构造函数
String(byte[ ] bytes):通过byte数组构造字符串对象。
String(char[ ] value):通过char数组构造字符串对象。
String(Sting original):构造一个original的副本。即:拷贝一个original。
String(StringBuffer buffer):通过StringBuffer数组构造字符串对象。
例如:
byte[] b = {‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’};
char[] c = {‘0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’};
String sb = new String(b); //abcdefghij
String sb_sub = new String(b,3,2); //de
String sc = new String(c); //0123456789
String sc_sub = new String(c,3,2); //34
String sb_copy = new String(sb); //abcdefghij
二、方法
char charAt(int index) :取字符串中的某一个字符,其中的参数index指的是字符串中序数。字符串的序数从0开始到length()-1 。
int compareTo(String anotherString) :当前String对象与anotherString比较。相等关系返回0;不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差,另一种情况,较长字符串的前面部分恰巧是较短的字符串,返回它们的长度差。
boolean contentEquals(StringBuffer sb) :将该String对象与StringBuffer对象sb进行比较。
StringBuffer sb = new StringBuffer("abc");
System.out.println("abc".equals(sb)); //false System.out.println("abc".contentEquals(sb)); //true
- static String copyValueOf(char[] data)
static String copyValueOf(char[] data, int offset, int count) :这两个方法将char数组转换成String,与其中一个构造函数类似。
byte[] getBytes() :将该String对象转换成byte数组。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) :该方法将字符串拷贝到字符数组中。其中,srcBegin为拷贝的起始位置、srcEnd为拷贝的结束位置、字符串数值dst为目标字符数组、dstBegin为目标字符数组的拷贝起始位置。
char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!
String s2 = new String("you!"); s2.getChars(0,3,s1,7); //s1=I love you!
- int indexOf(int ch) :只找第一个匹配字符位置。
- int indexOf(int ch, int fromIndex) :从fromIndex开始找第一个匹配字符位置。
- int indexOf(String str) :只找第一个匹配字符串位置。
int indexOf(String str, int fromIndex) :从fromIndex开始找第一个匹配字符串位置。
String replace(char oldChar, char newChar) :将字符号串中第一个oldChar替换成newChar。
String replace(CharSequence target, CharSequence replacement)
使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。String substring(int beginIndex) :取从beginIndex位置开始到结束的子字符串。
- 13.String substring(int beginIndex, int endIndex) :取从beginIndex位置开始到endIndex位置的子字符串。
char[ ] toCharArray() :将该String对象转换成char数组。
static String valueOf(boolean b)
- static String valueOf(char c)
- static String valueOf(char[] data)
- static String valueOf(char[] data, int offset, int count)
- static String valueOf(double d)
- static String valueOf(float f)
- static String valueOf(int i)
- static String valueOf(long l)
- static String valueOf(Object obj)
三、Java中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 startsWith(String prefix)
判断字符串是否以prefix字符串开头;
public boolean endsWith(String suffix)
判断一个字符串是否以suffix字符串结尾;
public String toUpperCase()
返回一个字符串为该字符串的大写形式;
public String toLowerCase()
返回一个字符串为该字符串的小写形式
public String substring(int beginIndex)
返回该字符串从beginIndex开始到结尾的子字符串;
public String substring(int beginIndex,int endIndex)
返回该字符串从beginIndex开始到endsIndex结尾的子字符串
public String trim()
返回该字符串去掉开头和结尾空格后的字符串
public String[] split(String regex)
将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组