Detailed explanation of Java's String class method
- 1. int length() returns the length of the string
- 2. char charAt(int index) returns the character at an index
- 3. boolean isEmpty() to determine whether it is an empty string
- 4. String toLowerCase() and String toUpperCase() convert all characters in String to case
- 5. String trim() returns a copy of the string ignoring leading and trailing blanks
- 6. boolean equals(Object obj) compares whether the contents of two strings are the same
- 7. String concat(String str) Concatenates the specified string to the end of this string
- 8. int compareTo(String anotherString) compares the size of two strings
- 9. String substring(int beginIndex) returns a new string, intercepting from beginIndex to the last substring
- 10. boolean endsWith(String suffix) Test whether this string ends with the specified suffix
- 11. boolean startsWith(String prefix) Test whether this string starts with the specified prefix
- 12. boolean contains(CharSequence s) returns true if and only if this string contains the specified sequence of char values
- 13. int indexOf()
- 14. String replace()
- 15. boolean matches(String regex) tells whether this string matches the given regular expression
- 16. String[] split(String regex) Splits this string based on matches of the given regular expression
- 17. Conversion between String and basic data types and wrapper classes
- 18. Conversion of String and char[]
- 19. Conversion of String and byte[]
1. int length() returns the length of the string
String s1 = "Hello World";
System.out.println(s1.length());
Output: 11
2. char charAt(int index) returns the character at an index
String s1 = "Hello World";
System.out.println(s1.charAt(0));
Output: H
3. boolean isEmpty() to determine whether it is an empty string
String s1 = "Hello World";
String s2 = "";
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
output:
false
true
4. String toLowerCase() and String toUpperCase() convert all characters in String to case
String s1 = "Hello World";
String s2 = s1.toLowerCase(Locale.ROOT);
System.out.println(s2);
String s3 = s2.toUpperCase(Locale.ROOT);
System.out.println(s3);
Output:
hello world
HELLO WORLD
5. String trim() returns a copy of the string ignoring leading and trailing blanks
String s3 = " Hello World ";
String s4 = s3.trim();
System.out.println("|"+s3+"|");
System.out.println("|"+s4+"|");
Output:
| Hello World |
|Hello World|
6. boolean equals(Object obj) compares whether the contents of two strings are the same
String s1 = "abc";
String s2 = "abe";
System.out.println(s1.equals(s2));
output:
false
boolean equalsIgnoreCase(String anotherString) is similar to the equals method to ignore case
7. String concat(String str) Concatenates the specified string to the end of this string
String s1 = "abc";
String s2 = "abe";
System.out.println(s1.concat(s2));
Output:
abcabe
8. int compareTo(String anotherString) compares the size of two strings
Pack the string into ASCII code and compare the size
String s1 = "abc";
String s2 = "abe";
System.out.println(s1.compareTo(s2));
Output:
-2 // If it is greater than 0, then s1 is larger; if it is less than 0, then s2 is larger; if it is equal to 0, it is equal
9. String substring(int beginIndex) returns a new string, intercepting from beginIndex to the last substring
- String substring(int beginIndex, int endIndex) returns a new string,
intercepted from beginIndex to endIndex
String s1 = "abcdef";
System.out.println(s1.substring(2));
System.out.println(s1.substring(2, 4));
Output:
cdef
cd
10. boolean endsWith(String suffix) Test whether this string ends with the specified suffix
String s1 = "abcdef";
System.out.println(s1.endsWith("ef"));
output:
true
11. boolean startsWith(String prefix) Test whether this string starts with the specified prefix
- boolean startsWith(String prefix, int toffset) Tests whether the substring of this string starting with the specified index starts with the specified prefix
String s1 = "abcdef";
System.out.println(s1.endsWith("ab"));
System.out.println(s1.startsWith("cd", 2));
output:
true
true
12. boolean contains(CharSequence s) returns true if and only if this string contains the specified sequence of char values
String s1 = "abcdef";
System.out.println(s1.contains("ef"));
System.out.println(s1.contains("ac"));
output:
true
false
13. int indexOf()
- int indexOf(String str) Returns the index of the first occurrence of the specified substring in this string
- int indexOf(String str, int fromIndex) Returns the index of the first occurrence of the specified substring in this string, starting from the specified index
String s1 = "abcdefabcdef";
System.out.println(s1.indexOf("f"));
System.out.println(s1.indexOf("f", 6));
5
11
- int lastIndexOf(String str) Returns the index of the rightmost occurrence of the specified substring in this string
- int lastIndexOf(String str, int fromIndex) Returns the index of the last occurrence of the specified substring in this string, starting the reverse search from the specified index
String s1 = "abcdefabcdef";
System.out.println(s1.lastIndexOf("a"));
System.out.println(s1.lastIndexOf("a", 4));
System.out.println(s1.lastIndexOf("ad"));
Output:
6
0
-1
14. String replace()
- String replace(char oldChar, char newChar) returns a new string obtained by replacing all occurrences of oldChar in this string with newChar
String s1 = "abcdefabcdef";
String s2 = s1.replace('a', 'b');
System.out.println(s2);
System.out.println(s1);
Output:
bbcdef bbcdef
abcdef abcdef
- String replace(CharSequence target, CharSequence replacement) Replaces all substrings of this string that match the literal target sequence with the specified literal replacement sequence
String s1 = "abcdefabcdef";
String s2 = s1.replace("ab", "ba");
System.out.println(s2);
System.out.println(s1);
bacdefbacdef
abcdefabcdef
- String replaceAll(String regex, String replacement) Replace all substrings of this string that match the given regular expression with the given replacement
String s1 = "abcdefabcdef";
String s2 = s1.replaceAll("abc", "cba");
System.out.println(s2);
System.out.println(s1);
cbadefcbadef
abcdefabcdef
- String replaceFirst(String regex, String replacement) replaces the first substring of this string matching the given regular expression with the given replacement
String s1 = "abcdefabcdef";
String s2 = s1.replaceFirst("abc", "cba");
System.out.println(s2);
System.out.println(s1);
cbadefabcdef
abcdefabcdef
15. boolean matches(String regex) tells whether this string matches the given regular expression
String s3 = "123";
// \\d+:表示是否有多个数字组成
boolean matches = s3.matches("\\d+");
System.out.println(matches);
String s4 = "021-3478929";
// 是否是由021-开头组成的7-8为数组
boolean matche = s4.matches("021-\\d{7,8}");
System.out.println(matche);
true
true
16. String[] split(String regex) Splits this string based on matches of the given regular expression
String s1 = "java|c++|python";
// 以“|”为分隔符,将字符串切分,返回字符串数组
String[] str = s1.split("\\|");
for (String string : str) {
System.out.println(string);
}
java
c++
python
- String[] split(String regex, int limit) Split this string according to matching the given regular expression, no more than limit. If it exceeds, all the rest will be placed in the last element
String s1 = "java|c++|python|go|php";
String[] str = s1.split("\\|", 3);
for (String string : str) {
System.out.println(string);
}
java
c++
python|go|php
17. Conversion between String and basic data types and wrapper classes
// string --> 基本数据类型,包装类Integer
String str = "123";
int num = Integer.parseInt(str);
System.out.println(num);
// 基本数据类型--->String
String str1 = String.valueOf(num);
String str2 = num + "";
System.out.println(str1);
System.out.println(str2);
18. Conversion of String and char[]
- String —> char[]: call toCharArray() method
// String 与 char[] 之间的转换
// String ---> char[]
String str = "abc123";
char[] chars = str.toCharArray();
for (char ch : chars) {
System.out.println(ch);
}
- char[]—>String: call String's char array constructor
// char[]--->String
char[] chars1 = new char[]{
'a', 'b', 'c', '1', '2', '3'};
// 调用String的char数组构造器
String str1 = new String(chars1);
System.out.println(str1);
19. Conversion of String and byte[]
- String —> byte[]: call getBytes()
// String ---> byte[]:调用getBytes()
String str = "abc123";
byte[] bytes = str.getBytes();
System.out.println(Arrays.toString(bytes));
String str0 = "abc123中国";
- byte[] —> String : Call the byte[] builder of String
// byte[] ---> String :调用String的byte[]造器
byte[] bytes1 = new byte[]{
97, 98, 99, 49, 50, 51};
String str2= new String(bytes1);
System.out.println(str2);