preface

String in Chinese is a String of characters. String is widely used in Java programming, in Java String does not belong to the basic type, belongs to the object, of course, for all Java code farmers, learn String basic operations is essential. The author is also picking up the basics of these foundations and taking notes. As the saying goes, a bad memory is better than a bad pen, but in this day and age, blogging is more realistic. Ha ha, start the text.

Java provides the String class to create and manipulate strings. Let’s analyze this using the method described in the Java String API documentation. [According to their own needs, press “Ctrl+F” to quickly query the required method]

String creation

1, the simplest and most crude way to create

Java simply inputs a constant of type String, and the compiler creates a String object with that value. This creates a string.

String str = "juejin.cn";
Copy the code

2. Construction method

Like all objects, strings can be created as objects. As follows:

String str = new String("juejin.cn");Copy the code

Of course, there are many ways to create a String object. There are more than ten ways to create a String object. This is just a list of the most common ways for your reference.

3, the difference between

Strings created by String are stored in the common pool, while String objects created by new are on the heap. Therefore, you should use reasonable string creation methods in your projects to reduce memory consumption.

String manipulation

1. Query the specified index

Char charAt(int index) Returns the char value at the specified index.

2. Compare this string to another object

Int compareTo(Object O) compares this string with another Object.

3. Compare two strings

Int compareTo(String anotherString) compares two strings in lexicographical order.

4. Compare two strings (case insensitive)

Int compareToIgnoreCase(String STR) Compares two strings in lexicographical order, regardless of case.

5. Concatenate strings

String concat(String STR) concatenates the specified String to the end of the String.

6. Check whether the string exists

Boolean contentEquals(StringBuffer sb) Returns true if and only if the string has characters in the same order as the specified StringBuffer.

7. Specify the sequence of strings in the array

Static String copyValueOf(char[] data) Returns a String representing the sequence of characters in the specified array.

8. Specify an array that represents the character sequence

Static String copyValueOf(char[] data, int offset, int count) Returns a String representing the sequence of characters in the specified array.

9. End of the specified suffix

Boolean endsWith(String suffix) whether this String endsWith the specified suffix.

10. Compare with the specified object

Boolean equals(Object anObject) compares this string with the specified Object.

11. String comparison (case insensitive)

Boolean equalsIgnoreCase(String anotherString) compares this String with anotherString, regardless of case.

12. Convert to byte arrays

Byte [] getBytes() encodes this String as a byte sequence using the platform’s default character set and stores the result in a new byte array.

13. Convert to a byte array

Byte [] getBytes(String charsetName) encodes this String as a byte sequence using the specified character set and stores the result in a new byte array.

Copy to the target character array

Void getChars(int srcBegin, int srcEnd, char[] DST, int dstBegin) copy characters from string to destination character array.

15, string hash code

Int hashCode() returns the hashCode for this string.

16. Index at first occurrence

Int indexOf(int ch) returns the index at the first occurrence of the specified character in this string.

17, the first occurrence of the specified character index

Int indexOf(int ch, int fromIndex) returns the index at the first occurrence of the specified character in this string, starting with the specified index.

18 Index at the first occurrence

Int indexOf(String STR) returns the indexOf the specified substring at its first occurrence in the String.

19. Index at first occurrence

Int indexOf(String STR, int fromIndex) returns the indexOf the specified substring at its first occurrence in the String, starting with the specified index.

20. Normalized representation

String Intern () returns the normalized representation of a String object.

21, the index at the last occurrence

Int lastIndexOf(int ch) returns the index at the last occurrence of the specified character in this string.

The index where 22 was last seen

Int lastIndexOf(int ch, int fromIndex) returns the index at the last occurrence of the specified character in the string, performing a reverse search starting at the specified index.

23. Index of the right-most occurrence

Int lastIndexOf(String STR) returns the index at the rightmost occurrence of the specified substring in the String.

Get the index at the last occurrence

Int lastIndexOf(String STR, int fromIndex) returns the index at the last occurrence of the specified substring in the String, searching backwards from the specified index.

25. The length of the string

Int length() returns the length of this string.

26. Whether to match the given regular expression

Boolean matches(String regex) Whether the String matches the given regular expression.

27. Whether the two string regions are equal

Boolean regionMatches(Boolean ignoreCase, INT ToffSet, String Other, INT OOFFSet, int Len) Specifies whether two String regions are equal.

28. Are the two string regions equal

Boolean regionMatches(int ToffSet, String Other, INT OOFFSet, int Len) Specifies whether two String regions are equal.

29. Replace the string

String replace(char oldChar, char newChar) returns a new String obtained by replacing all occurrences of oldChar in this String with newChar.

The re replaces all strings

String replaceAll(String regex, String replacement) replaces all substrings of this String that match the given regular expression with the given replacement.

31. Re replacement string

String replaceFirst(String regex, String replacement) replaces the first substring of this String matching the given regular expression with the given replacement.

32 Regular expression matches split this string

String[] split(String regex) Splits this String based on the match of the given regular expression.

Regular expression to split the string

String[] split(String regex, int limit) splits the String based on matching the given regular expression.

34 Start with the specified prefix

Boolean startsWith(String prefix) tests whether the String startsWith the specified prefix.

35, specify prefix start

Boolean startsWith(String prefix, int toffSET) Specifies whether substrings starting with the specified index start with the specified prefix.

Get character sequence by index

CharSequence subSequence(int beginIndex, int endIndex) returns a new sequence of characters, which is a subSequence of this sequence.

Gets the specified string by index

String SubString (int beginIndex) returns a new String, which is a substring of the String.

Gets the specified string by index

String SubString (int beginIndex, int endIndex) returns a new String, which is a substring of the String.

39. String to array

Char [] toCharArray() converts this string to a new character array.

40, Lowercase String toLowerCase()

Convert all characters in this String to lowercase using the rules of the default locale.

41. Change the string to lowercase according to the specified rule

String toLowerCase(Locale Locale) converts all characters in a String toLowerCase using the rules of the given Locale.

42, The object itself string

String toString() returns the object itself (it is already a String!). .

43. Change the string to uppercase

String toUpperCase() converts all characters in this String toUpperCase using the rules of the default locale.

44. Capitalize the regular string

String toUpperCase(Locale Locale) converts all characters in this String toUpperCase using the rules of the given Locale.

45. Remove Spaces at the beginning and end of strings

String trim() returns a copy of the String, ignoring leading and trailing whitespace.

Return a string of the given data type

Static String valueOf(Primitive Data type x) Returns a String representation of the parameter x of the given data type.

47, Check whether the specified character is included

Contains (CharSequence chars) Determines whether the specified character series is contained.

48. Check whether the string is empty

IsEmpty () checks if the string isEmpty.

Third, summary

This article is based on the Java String class API document for an overall analysis, you can according to their own project needs, the relevant one or more API related methods, secondary encapsulation, as a tool class for repeated use. Of course, the way the String class operates has changed over time with the JDK. Call related functions according to the different versions used in the project.

Well, thank you for reading, I hope you like it, if it is helpful to you, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.