What is the String?
The String class represents a String. All string literals (such as “ABC”) in Java programs are implemented as instances of this class. Strings are immutable; Their values cannot be changed after creation. String buffers support mutable strings. Because strings are immutable, they can be shared. For example: String STR = “ABC “; The String class includes various characters to examine sequences, to compare strings, to search strings, to extract substrings, and to create a copy of a String with all characters translated to upper or lower case.
The basic way to
1. Obtain the 1.1 character string length.
int length(a): Gets the length.Copy the code
Int length() : gets the length. 1.2 Obtains a character in a position based on the position.
char charAt(int index): Returns the character at the specified index.Copy the code
Char charAt(int index) : Returns the character at the specified index. 1.3 Obtaining the position of a character in a string by character. Returns -1 if not found.
int indexOf(int ch): Finds the position where the character first appearsint indexOf(intCh,int fromIndex): Index from fromIndex.int indexOf(String str)
int indexOf(String str,int fromIndex): Retrieves the position of the string STR from fromIndex.Copy the code
Conversely, if you need a reverse index, there is a similar approach
int lastIndexOf(*)Index from right to left, and then print its position in the string array **Copy the code
2. Check whether 2.1 contains a substring.
boolean contains(CharSequence str); ★ Special:intIndexOf (STR) can also retrieve the presence of STR.Copy the code
2.2 Check whether the String contains content.
boolean isEmpty(a): The principle is to determine whether length is 0Copy the code
2.3 Whether the string starts with the specified content.
boolean startWith(String str);
2.4 Whether the character string ends with the specified content
boolean startWith(String str);
Copy the code
Application scenario: Search for required files based on the file name
2.5 Checking whether the Contents of the Character Strings are the same
boolean equals(str)
Copy the code
2.6 Determine whether the contents are the same and ignore case
boolean equalsIgnoreCase(str)
Copy the code
Conversion 3.1 Converts a character array into a string constructor
String (charString ([])char[],offset,count) converts part of a character array to a stringCopy the code
Ex. :
char[] arr = {'a'.'b'.'c'.'d'.'e'.'f'};
String s = new String(arr,1.3);
Copy the code
The result will be BCD. Since the subscript of the array starts at 0, it starts with b and takes 3 characters, which is BCD.
Static methods: need to be used after a String
static String copyValueOf(char[]);
static String copyValueOf(char[],offset,count);
Copy the code
3.2 Converting a string into a character array
char[] toCharArray();
Copy the code
3.3 Converting byte arrays into strings
String (byte[]); String (byte[], offset, count);Copy the code
3.4 Converting strings into byte arrays
byte[] getBytes();
Copy the code
3.5 Converting basic data types to strings
static Sting valueOf(a);
Copy the code
Special: Strings and byte arrays can specify encoding tables during conversion
4. Replace
String replace(old char/str,new char/str);Copy the code
Note: Since strings cannot be changed once they are created, this substitution will return a copy of the new string, and the original string will not be transformed
5. Cutting
String[] split(regex) Splits with regex as the split symbolCopy the code
6. Substring
String substring(begin); From the beginning to the end of the assignmentString substring(begin,end); A substring starting at begin and ending at end. ** includes begin but not end.**Copy the code
case
String s = "abcdef";
s1 = s.substring(2);
s2 = s.substring(2.4); S1 = cdef result, s2 = CD.Copy the code
7. Conversion 7.1 Converting a String to uppercase or lowercase
String toUpperCase(a);
String toLowerCase(a);
Copy the code
7.2 Remove Spaces at both ends of a String
String trim(a);
Copy the code
7.3 Comparison of Two Strings in a natural order
int compareTo(str); ** Compares two string sizes and returns the ASCII value difference between the first different character. **Copy the code
public class StringClass judgment function{
public static void main(String[] args) {
// Define three strings;
String a = "linkinPark";
String b = "linkinPark";
String c = "LinKinPark";
// Boolean equals(Object obj): Compares this string with the specified Object.
System.out.println(a.equals(b));//true
System.out.println(a.equals(c));//false
System.out.println("-- -- -- -- -");
// Boolean equalsIgnoreCase(String STR): Compares this String with another String, ignoring case.
System.out.println(a.equalsIgnoreCase(b));//true
System.out.println(a.equalsIgnoreCase(c));//true
System.out.println("-- -- -- -- -");
// Boolean contains(String STR): determines whether this String contains the STR substring.
System.out.println(a.contains("Park"));//true
System.out.println(a.contains("Lin"));//false
System.out.println("-- -- -- -- -");
// Boolean startsWith(String STR): checks whether the String startsWith STR substring.
System.out.println(a.startsWith("lin"));//true
System.out.println(a.startsWith("Lin"));//false
System.out.println("-- -- -- -- -");
// Boolean endsWith(String STR): Checks whether STR has ended the String.
System.out.println(a.endsWith("Park"));//true
System.out.println(a.endsWith("park"));//false
System.out.println("-- -- -- -- -");
// Boolean isEmpty(): checks whether the string isEmpty.
String e = "";
System.out.println(e.isEmpty());//false
String f = null;//java.lang.NullPointerException
// Add a non-null judgment
if(f! =null){ System.out.println(f.isEmpty()); }}}Copy the code
Time to talk:
If the guest officer feel edible appropriate can give a free praise! Thanks, thanks! Go slow, guest officer! I suggest you pack it and come back next time. Shopkeeper QQ: 309021573, welcome harassment!