Home >Java >javaTutorial >String common API
API(Application Programming Interface): Application Programming Interface
Use Scanner to obtain the string entered by the keyboard
next(); When a space is encountered, it will be judged as The content after the ending space of the current input will not be received.
nextLine(); can avoid being interrupted by spaces, but it will be interfered when receiving numbers are used together.
Create characters String object
public String(String original) ---> Create a string object through a string
public String(char[] value) -=--> Create through a character array A string object
public String(char[] value,int offset,int count)---> Create a string object from a part of the character array starting from the index value offset and continuing for count
==
== means judging whether both sides are equal
The basic type judgment is the numerical value
The reference type judgment is the address
Passed The difference between a string object created by a constructor and a string object created by direct assignment:
The constructor creates a string object in the heap area and the direct assignment creates a string object in the constant pool
Judgement function
boolean equals(Object obj) // To compare whether the contents of strings are equal, write the string with which string the parameter is compared
boolean equalsIgnoreCase(String str) // When comparing whether the contents of strings are equal Ignore case
boolean startsWith(String str) // Determine whether the string starts with str
boolean endsWith(String str) // Determine whether the string ends with str
Obtained Function
int length() // Get the length of the string (get the number of characters in the string)
char charAt(int index) // Return the corresponding character according to the specified index
int indexOf(String str) // Get the index value where str appears in the string. If there is no str in the string, return -1
String substring(int start) // From the index value of start to the end of the string Intercept it and return it as a new string
String substring(int start, int end) // Intercept the string starting from start and end-1 to generate a new string and return
statistical characters The number of uppercase, lowercase and numeric characters in the string
* String s1 = "aAb0G9c1Kde";
* String traversal Judgment Define counters to represent the number of three types of characters
* 1. Definition 3 Each counter represents the number of three types of characters respectively
* 2. Traverse the string to obtain each character for judgment
* 3. Once a certain character is matched, let the corresponding counter +1
1 public static void main(String[] args) { 2 // TODO Auto-generated method stub 3 String s1 = "aAb0G9c1Kde"; // 4 // 1.定义三个计数器分别代表三种字符的个数 5 int big = 0; 6 int small = 0; 7 int num = 0; 8 // 2.遍历字符串获取每个字符进行判断 9 for (int i = 0; i < s1.length(); i++) {10 char ch = s1.charAt(i);11 if (ch >= '0' && ch <= '9') {12 num++;13 } else if (ch >= 'A' && ch <= 'Z') {14 big++;15 } else if (ch >= 'a' && ch <= 'z') {16 small++;17 }18 }19 }
Conversion function method
char[] toCharArray() ---> Convert the string into an array and return it
String toLowerCase() ---> Convert the data in the string to lowercase letters and return
String toUpperCase() ---> Convert the data in the string to uppercase letters and return
Remove spaces And split function method
String trim() //Remove the spaces on both sides of the string, the spaces in the middle of the string will not be removed
String[] split(String str)//Use the string as str Carry out cutting, and form the string obtained after cutting into a string array to return
String replaceAll(String regex,String replacement)
boolean contains(String str);
String replaceAll(String regex, String replacement)// Use replacement to replace all regex in the string
boolean contains(String str);// Determine whether the string contains str. As long as there are Str in the string, it will return true regardless of how many. If there is not, it will return false
StringBuilder construction method
StringBuilder() ---> Create an empty SB container
StringBuilder(String str)
StringBuilder common methods
public int capacity()//The capacity of the container
public int length() //The actual number of characters stored
StringBuilder methods
public StringBuilder append (any type) // Any type of data can be added to this container, and it will eventually be reflected in the form of a string. After the call is completed, it will be returned
public StringBuilder reverse() // Flip the contents of the container
The above is the detailed content of String common API. For more information, please follow other related articles on the PHP Chinese website!