12 commonly used methods of String class

1. Create a String object with the character array value
Method:

public String(char[] value)

Example:

char[] value ={"a","b","c","d"};
String str = new String(value);
//相当于String str = newString("abcd")

2. Create a String object with n characters starting with x from the character array
Method:

public String(char chars[], int x, int n)

Example:

char[] value ={"a","b","c","d"};
String str = new String(value, 1, 2);
//相当于String str = newString("bc");

3. Get the string length
Method:

 public int length()

Example:

String str = new String("478bhjd56");
int strlength = str.length();

4. Get the character at a certain position in the string
Method:

public char charAt(int index)

Example:

String str = new String("43dfzyd");
char ch = str.charAt(4);//ch = z

Note: The first character index in the string is 0, the last one is length()-1.

5. Get a substring of a string
Method:

public String substring(int beginIndex)
//该方法从beginIndex位置起,
//从当前字符串中取出剩余的字符作为一个新的字符串返回。

public String substring(int beginIndex, intendIndex)
//该方法从beginIndex位置起,从当前字符串中
//取出到endIndex-1位置的字符作为一个新的字符串返回。

Example:

String str1 = newString("asdfzxc");
String str2 = str1.substring(2);//str2 ="dfzxc"
String str3 = str1.substring(2,5);//str3 ="dfz"

6. String comparison

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方法相似,但忽略大小写。

Example:

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. Find the position of a substring in a string
Method:

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. Case conversion of characters in strings
Method:

public String toLowerCase()
//返回将当前字符串中所有字符转换成小写后的新串

public String toUpperCase()
//返回将当前字符串中所有字符转换成大写后的新串

Example:

String str = new String("JavaStudy");
String str1 = str.toLowerCase();
//str1 = "javastudy"
String str2 = str.toUpperCase();
//str2 = "JAVASTUDY"

9. Remove spaces at both ends of the string

String trim()
//去除字符串两端的空格,中间的空格不变,一般用于登陆注册时

Example:

String str = " z dali ";
String str1 = str.trim();
int a = str.length();//a = 8
int b = str1.length();//b = 6
System.out.println(a+"\n"+b);

10. Split the string into a string array
Method:

String[] split(String str)

Example:

String str = "sgs#jkc#eer";
String[] str1 = str.split("#");
for (int i = 0; i < str1.length; i++) {
  System.out.println(str1[i]);  
  //输出结果是sgs  jkc eer
}

11. The basic type is converted to a string
method:

static String valueOf(xxx xx)

Example:

String s1 = String.valueOf(12.99);
System.out.println(s1);
//double类型转string类型

12. Replace string
method:

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相匹配的子串,应将新的字符串返回。

Example:

String str = "hjdfjdskdd";
String str1 = str.replace('h','g');
//str1 ="gjdfjdskdd"
String str2 =str.replace("hj","xxx");
//str2 = "xxxdfjdskdd"
String str3 =str.replaceFirst("d","c");
//str3 = "hjcfjdskdd"
String str4 =str.replaceAll("hj","xxx");
//str4 = "xxxdfjdskdd"
System.out.println(str1+"\n"+str2+"\n"+str3+"\n"+str4);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860185&siteId=291194637