The acquisition function of String class: the basic acquisition function of String class, examples of acquisition function, basic conversion function of String class, examples of conversion function,
1. The acquisition function of String class:
(1) int length()
Gets the length of the string, that is, the number of characters in the string.
(2) char charAt(int index)
Gets the characters at the specified index position.
(3) int indexOf(int ch)
Gets the index of the specified character that first appears in this string. Note: Int is used here, not char, because both 'a' and 97 can be passed in as real parameters.
(4) int indexOf(String str)
Gets the index of the first occurrence of the specified string in this string.
(5) int indexOf(int ch,int fromIndex)
Gets the index that appears for the first time after the specified position in this string.
(6) int indexOf(String str,int fromIndex)
Gets the index that appears for the first time after the specified position in this string.
(7) String substring(int start)
Intercept substrings from the specified position, by default, to the end. (Including start location)
(8) String substring(int start,int end)
Seize the substring from the start of the specified position to the end of the specified position. (The package start does not include end)
2. Examples of obtaining functions
package cn.itcast_06; public class StringDemo { public static void main(String[] args) { // int length() // Get the length of the string, that is, the number of characters in the string. String s="helloworld"; System.out.println("length():"+s.length());//10 System.out.println("-------------------------"); // char charAt(int index) // Get the characters at the specified index position. System.out.println("charAt:"+s.charAt(0));//h System.out.println("charAt:"+s.charAt(9));//d System.out.println("----------------------------"); // int indexOf(int ch) // Get the index of the specified character that appears for the first time in this string. Note: Int is used here, not char, // The reason is that both 'a' and 97 can be passed in as real parameters. System.out.println("indexOf:"+s.indexOf('h'));//0 System.out.println("indexOf:"+s.indexOf('d'));//9 System.out.println("------------------------"); // int indexOf(String str) // Get the index that appears for the first time in this string. System.out.println("indexOf:"+s.indexOf("owo"));//4 System.out.println("indexOf:"+s.indexOf("ld"));//8 System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // int indexOf(String str,int fromIndex) // Get the index that appears for the first time after the specified position in this string. System.out.println("indexOf:"+s.indexOf('l',4));//8 System.out.println("indexOf:"+s.indexOf('l',40));//-1 System.out.println("-----------------------"); // String substring(int start) // Intercept substring from the specified position, the default is to intercept to the end. (Including the start position) System.out.println("substring:"+s.substring(4));//oworld System.out.println("substring:"+s.substring(0));//helloworld // String substring(int start,int end) // Start from the specified position to the end of the specified position. (The package start does not include end) System.out.println("substring:"+s.substring(4,8));//owor System.out.println("substring:"+s.substring(0,s.length()));//helloworld } }3. String conversion function:
(1) byte[ ] getBytes( )
Convert strings to byte arrays.
(2) char[ ] toCharArray( )
Convert strings to character arrays.
(3) static String valueOf(char[ ] chs)
Converts a character array into a string.
(4) static String valueOf(int i)
Convert int type data into a string.
Note: The valueOf method of the String class can convert any type of data into a string.
(5) String toLowerCase( )
Convert the string to lower case.
(7) String toUpperCase( )
Convert the string to capitalization.
(8) String concat(String str)
Stitch the strings. Use + is OK.
4. Examples of conversion functions of String class:
package cn.itcast_06; public class StringDemo4 { public static void main(String[] args) { // Define a string object String s = "JavaSE"; // byte[] getBytes(): Convert a string to a byte array. byte[] bys = s.getBytes(); for (int x = 0; x < bys.length; x++) { System.out.println(bys[x]); } System.out.println("-------------------------------------"); // char[] toCharArray(): Convert a string to a character array. char[] chs = s.toCharArray(); for (int x = 0; x < chs.length; x++) { System.out.println(chs[x]); } System.out.println("----------------------------------"); // static String valueOf(char[] chs): Convert a character array into a string. String ss = String.valueOf(chs); System.out.println(ss); System.out.println("----------------------------------"); // static String valueOf(int i): Convert data of type int into a string. int i = 100; String sss = String.valueOf(i); System.out.println(sss); System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("toLowerCase:" + s.toLowerCase()); System.out.println("s:" + s); System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("toUpperCase:" + s.toUpperCase()); System.out.println("s:" + s); System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- String s1 = "JavaSE"; String s2 = "JavaEE"; String s3 = s1 + s2; String s4 = s1.concat(s2); System.out.println("s3:"+s3); System.out.println("s4:"+s4); } }Replenish:
The following is the acquisition function of the String class
package string; //Getting function of String class public class StringDemo { public static void main(String[] args) {//Define a string object String s="helloworld";//Return the length of the string System.out.println("s.length="+s.length());//Get the index character at the specified position System.out.println("charAt:"+s.charAt(7));// Returns the index System.out.println("indexOf:"+s.indexOf('l'));//Returns the index System.out.println("indexOf:"+s.indexOf("owo"));//Returns the index System.out.println("indexOf:"+s.indexOf("owo"));//Returns the index System.out.println("indexOf:"+s.indexOf('l',4));//Not found or If it does not exist, it returns -1//Returns the index that appears for the first time in this string from the specified position System.out.println("indexOf:"+s.indexOf("ell",4));//Seave System.out.println("substring:"+s.substring(2));//Seave from the specified position to the specified position (close front and open back) System.out.println("substring:"+s.substring(2,8)); } }Summarize
The above is the acquisition and conversion function of the String class introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!