1. 用字符数组value创建一个String对象
public String(char[] value)
实例:
char[] value ={"a","b","c","d"};
String str = new String(value);
2. 用字符数组以x开始的n个字符创建一个String对象
public String(char chars[], int x, int n)
实例:
char[] value ={"a","b","c","d"};
String str = new String(value, 1, 2);
3. 获取字符串长度
public int length()
实例:
String str = new String("478bhjd56");
int strlength = str.length();
4. 获取字符串某一位置的字符
public char charAt(int index)
实例:
String str = new String("43dfzyd");
char ch = str.charAt(4);//ch = z
5.获取字符串的子串
public String substring(int beginIndex)
public String substring(int beginIndex, intendIndex)
实例:
String str1 = newString("asdfzxc");
String str2 = str1.substring(2);//str2 ="dfzxc"
String str3 = str1.substring(2,5);//str3 ="dfz"
6.字符串的比较
public int compareTo(String str)
//该方法是对字符串内容按字典顺序进行大小比较,
//通过返回的整数值指明当前字符串与参数字符串的大小关系。
//若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
public int compareToIgnoreCase (String str)
//与compareTo方法相似,但忽略大小写。
public boolean equals(Object obj)
//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
public boolean equalsIgnoreCase(String str)
//与equals方法相似,但忽略大小写。
实例:
String str1 = new String("abc");
String str2 = new String("ABC");
int a = str1.compareTo(str2);//a=32
int b = str1.compareToIgnoreCase(str2);//b=0
boolean c = str1.equals(str2);//c=false
boolean d =str1.equalsIgnoreCase(str2);//d=true
7.查找子串在字符串中的位置
public int indexOf(String str)
//用于查找当前字符串中字符或子串,返回字符或
//子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
public int indexOf(String str, intfromIndex)
//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
public int lastIndexOf(String str)
//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
public int lastIndexOf(String str, intfromIndex)
//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
8.字符串中字符的大小写转换
public String toLowerCase()
//返回将当前字符串中所有字符转换成小写后的新串
public String toUpperCase()
//返回将当前字符串中所有字符转换成大写后的新串
9.字符串两端去空格
String trim()
10.将字符串分割成字符串数组
String[] split(String str)
11.基本类型转换为字符串
static String valueOf(xxx xx)
12.替换字符串
public String replace(char oldChar, charnewChar)
//用字符newChar替换当前字符串中所有的oldChar字符,
//并返回一个新的字符串。
public String replaceFirst(String regex,String replacement)
//该方法用字符replacement的内容替换当前字符串中遇到的
//第一个和字符串regex相匹配的子串,应将新的字符串返回。
public String replaceAll(String regex,String replacement)
//该方法用字符replacement的内容替换当前字符串中遇到的所有
//和字符串regex相匹配的子串,应将新的字符串返回。