Java String class Strings are widely used in Java programming. Strings are objects in Java. Java provides the String class to create and operate strings. Today I will introduce to you some common string manipulation methods in Java.
1. Use the length() method to get the length of the string
public class maintest { public static void main(String[] args) { String str = "abcdefg"; //length():统计当前字符串的字符个数 int i = str.length(); System.out.println(i); } }
2. Use the indexOf() method to find the position of the specified character in the string
public class maintest { public static void main(String[] args) { String str = "abcdefg"; //indexOf():查找指定字符再字符串中的位置 int index = str.indexOf("a"); System.out.println(index); } }
3. Use the toUpperCase() method to change the characters in the string to uppercase
public class maintest { public static void main(String[] args) { String str = "abcdefg"; //小写转大写 //toUpperCase():将字符串中的字符变为了大写形式 String str = str.toUpperCase(); System.out.println(str); } }
4. Use the toLowerCase() method to change the characters in the string to lowercase
public class maintest { public static void main(String[] args) { //toLowerCase():将字符串中的字符变为小写 String str = "WWMMDDHH"; String str1 = str3.toLowerCase(); System.out.println(str); } }
5. Use the substring() method to intercept the string
public class maintest { public static void main(String[] args) { String str = "abcdefg"; //substring:截取字符串 String str = "abcdefg"; String str = str5.substring(0, 3); System.out.println(str); String str = str5.substring(3); System.out.println(str); } }
6. Use the replaceAll() Method to replace the specified content in the current string
public class maintest { public static void main(String[] args) { String str = "abcdefg"; String str = str5.replaceAll("abc", "xyz"); System.out.println(str); } }
7. Use the trim() method to remove the spaces at both ends of the current string
public class maintest { public static void main(String[] args) { String str = " abc def "; System.out.println(str.length()); String str1 = str9.trim(); System.out.println(str); System.out.println(str1); } }
8. String string is equal to splicing
public class maintest { public static void main(String[] args) { String str1 = "123"; String str2 = "100"; System.out.println(str1+str2); } }
9. Convert a string into an integer
public class maintest { public static void main(String[] args) { String str1 = "123"; String str2 = "100"; int num1 = Integer.parseInt(str1); int num2 = Integer.parseInt(str2); System.out.println(num1+num2); } }
10. Use charAt ()Find the character at the specified position in the string
public class maintest { public static void main(String[] args) { String str1000 = "abcde"; //charAt:找到指定字符串中位置的字符 char char = str1000.charAt(2); System.out.println(char); } }
The above is the string operation commonly used in Java that I have summarized for everyone. If you want to know more about java, you can click: java tutorial
The above is the detailed content of Common string operations in java. For more information, please follow other related articles on the PHP Chinese website!