Home > Java > javaTutorial > What are the common methods of string class in Java?

What are the common methods of string class in Java?

青灯夜游
Release: 2020-07-27 11:54:31
Original
10654 people have browsed it

Commonly used methods of the string class are: length(), charAt(), substring(), compareTo(), compareToIgnore(), equals(), concat(), indexOf(), lastIndexOf(), etc. .

What are the common methods of string class in Java?

1. String class

The String class is in the java.lang package, java Use the String class to create a string variable. String variables belong to objects. Java declares the String class as a final class and cannot have classes. String class objects cannot be modified after they are created. They consist of 0 or more characters and are contained between a pair of double quotes.

2. Creation of String class objects

String declaration: String stringName;

String creation: stringName = new String (string constant); or stringName = string constant;

3. String class construction method

1, public String()

No-argument constructor, used to create a String object of an empty string.

String str1 = new String();
Copy after login

2, public String(String value)

Create a String object using a known string value.

String str2 = new String("asdf"); 
String str3 = new String(str2);
Copy after login

3, public String(char[] value)

Create a String object using the character array value.

char[] value = {'a','b','c','d'};
String str4 = new String(value);//相当于String str4 = new String("abcd");
Copy after login

4, public String(char chars[], int startIndex, int numChars)

Create a String object using numChars characters starting from startIndex of the character array chars .

char[] value = {'a','b','c','d'};
String str5 = new String(value, 1, 2);//相当于String str5 = new String("bc");
Copy after login

5, public String(byte[] values)

Create a String object using bit array values.

byte[] strb = new byte[]{65,66};
String str6 = new String(strb);//相当于String str6 = new String("AB");
Copy after login

4. Commonly used methods of String class

1. Find the length of the string

public int length( )//Return the length of the string

String str = new String("asdfzxc");
int strlength = str.length();//strlength = 7
Copy after login

2. Find the character at a certain position in the string

public char charAt(int index)//Return The character at the specified position in the string; note that the first character index in the string is 0 and the last one is length()-1.

String str = new String("asdfzxc");
char ch = str.charAt(4);//ch = z
Copy after login

3. Extract substring

Use the substring method of the String class to extract the substring in the string. This method has two common parameters:

1)public String substring(int beginIndex)//This method takes out the remaining characters from the current string starting from the beginIndex position and returns it as a new string.

2)public String substring(int beginIndex, int endIndex)//This method starts from the beginIndex position and takes the characters from the current string to the endIndex-1 position as a new String returned.

String str1 = new String("asdfzxc");
String str2 = str1.substring(2);//str2 = "dfzxc"
String str3 = str1.substring(2,5);//str3 = "dfz"
Copy after login

4. String comparison

1)public int compareTo(String anotherString)//This method compares the string contents in dictionary order. The returned integer value indicates the size relationship between the current string and the parameter string. If the current object is larger than the parameter, a positive integer is returned, otherwise a negative integer is returned, and 0 is returned if equal.

2)public int compareToIgnore(String anotherString)//Similar to the compareTo method, but ignores case.

3)public boolean equals(Object anotherObject)//Compare the current string and the parameter string, return true when the two strings are equal, otherwise return false.

4)public boolean equalsIgnoreCase(String anotherString)//Similar to the equals method, but ignores case.

String str1 = new String("abc");
String str2 = new String("ABC");
int a = str1.compareTo(str2);//a>0
int b = str1.compareToIgnoreCase(str2);//b=0
boolean c = str1.equals(str2);//c=false
boolean d = str1.equalsIgnoreCase(str2);//d=true
Copy after login

5. String concatenation

public String concat(String str)//Concatenate the string str in the parameter to the end of the current string, the effect is etc. Priced at " ".

String str = "aa".concat("bb").concat("cc");
//相当于String str = "aa"+"bb"+"cc";
Copy after login

6. Search for a single character in a string

1)public int indexOf(int ch/String str)//Used to find characters in the current string or Substring, returns the position where the character or substring first appears from the left in the current string, or -1 if it does not appear.

2)public int indexOf(int ch/String str, int fromIndex)//The modified method is similar to the first one, the difference is that this method searches backward from the fromIndex position.

3)public int lastIndexOf(int ch/String str)//This method is similar to the first one, except that this method searches forward from the end of the string.

4)public int lastIndexOf(int ch/String str, int fromIndex)//This method is similar to the second method, except that this method searches forward from the fromIndex position.

String str = "I am a good student";
int a = str.indexOf('a');//a = 2
int b = str.indexOf("good");//b = 7
int c = str.indexOf("w",2);//c = -1
int d = str.lastIndexOf("a");//d = 5
int e = str.lastIndexOf("a",3);//e = 2
Copy after login

7. Case conversion of characters in string

1)public String toLowerCase()//Return after converting all characters in the current string to lowercase The new string

2)public String toUpperCase()//Returns the new string after converting all characters in the current string to uppercase

String str = new String("asDF");
String str1 = str.toLowerCase();//str1 = "asdf"
String str2 = str.toUpperCase();//str2 = "ASDF"
Copy after login

8. String Replacement of characters in

1)public String replace(char oldChar, char newChar)//Replace all oldChar characters in the current string with the character newChar and return a new string .

2)public String replaceFirst(String regex, String replacement)//This method uses the content of character replacement to replace the first character encountered in the current string that matches the string regex substring, a new string should be returned.

3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。

String str = "asdzxcasd";
String str1 = str.replace('a','g');//str1 = "gsdzxcgsd"
String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"
String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"
String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"
Copy after login

9、其他类方法

1)String trim()//截去字符串两端的空格,但对于中间的空格不处理。

String str = " a sd ";
String str1 = str.trim();
int a = str.length();//a = 6
int b = str1.length();//b = 4
Copy after login

2)boolean statWith(String prefix)boolean endWith(String suffix)//用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。

String str = "asdfgh";
boolean a = str.statWith("as");//a = true
boolean b = str.endWith("gh");//b = true
Copy after login

3)regionMatches(boolean b, int firstStart, String other, int otherStart, int length)//从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。

4)contains(String str)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。

String str = "student";
str.contains("stu");//true
str.contains("ok");//false
Copy after login

5)String[] split(String str)//将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。

String str = "asd!qwe|zxc#";
String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";
Copy after login

五、字符串与基本类型的转换

1、字符串转换为基本类型

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)

例如:

int n = Integer.parseInt("12");
float f = Float.parseFloat("12.34");
double d = Double.parseDouble("1.124");
Copy after login

2、基本类型转换为字符串类型

String类中提供了String valueOf()放法,用作基本类型转换为字符串类型。

1)static String valueOf(char data[])

2)static String valueOf(char data[], int offset, int count)

3)static String valueOf(boolean b)

4)static String valueOf(char c)

5)static String valueOf(int i)

6)static String valueOf(long l)

7)static String valueOf(float f)

8)static String valueOf(double d)

例如:

String s1 = String.valueOf(12);
String s1 = String.valueOf(12.34);
Copy after login

3、进制转换

使用Long类中的方法得到整数之间的各种进制转换的方法:

Long.toBinaryString(long l)
Long.toOctalString(long l)
Long.toHexString(long l)
Long.toString(long l, int p)//p作为任意进制
Copy after login

推荐教程:《java教程

The above is the detailed content of What are the common methods of string class in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template