“This is the sixth day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”
In this installment, we’re going to look at String methods. If we look at the API, we see that there are dozens of String methods. Do we need to write them all down? Of course not. Write it all down and our heads will explode. We just need to remember a few common methods and consult the API documentation for the rest when we use them. Let’s take a look at some of the commonly used methods in the String class.
Comparison method.
We know that in Java “==” compares the address values of two objects, but what if we want to compare the contents of strings?
We can use two methods, the equals method and the compareTo method
Equals public Boolean equals(Object anObject) Compares this string to the specified Object. The result is true if and only if the argument is not NULL and is a String representing the same sequence of characters as this object. Parameter: anObject - The object to be compared with this String. Return: true if the String represented by the given object is equal to this String; Otherwise return falseCopy the code
CompareTo public int compareTo(String anotherString) compareTo(String anotherString) Compares two strings in lexicographical order. If the String precedes the argument String in lexicographical order, the result of the comparison is a negative integer. If the String comes after the argument String in lexicographical order, the result of the comparison is a positive integer. If the two strings are equal, the result is 0; CompareTo returns 0 only if the equals(Object) method returns true. Parameter: anotherString - The String to be compared. Return: if the argument string is equal to this string, return the value 0; If the string is lexicographically smaller than the string argument, return a value less than 0; Returns a value greater than 0 if the string is greater than the string argument in lexicographical order.Copy the code
Both methods can be used to compare strings for equality. The difference between the two methods is that the return value of the equals method is a Boolean, and the return value of the compareTo method is an int.
String retrieves related methods
Gets the character at the specified index of the string
CharAt public char charAt(int index) Returns the char value at the specified index. The index ranges from 0 to length() -1. The first char value of the sequence is at index 0, the second at index 1, and so on, similar to an array index. Parameter: index-char Specifies the index of the value. Return: This string specifies the char value at the index. The first char value is at index 0. Throws: IndexOutOfBoundsException - if the index parameter is negative or less than the length of the string.Copy the code
Gets the length of the string
Length public int length() Returns the length of the string. Returns the length of the sequence of characters represented by this object.Copy the code
Concatenated string
We know that you can use the + sign to add more characters to a string, and Java provides ways to concatenate strings specifically. Concat public String concat(String STR) concatenates the specified String to the end of the String. Parameter: STR - String connected to the end of this String. Returns: a string representing the character of the object character followed by the string parameter character.Copy the code
The method returns a string, followed by the string the method was called with the string arguments passed in.
Gets the index of the specified character in the string
IndexOf public int indexOf(int ch) returns the indexOf the first occurrence of the specified character in this string. Parameter: CH - A character. Returns the index of the character for the first time in the sequence of characters represented by this object; If the character does not occur, -1 is returned. -- indexOf public int indexOf(int ch, int fromIndex) Returns the index at the first occurrence of the specified character in the string. The search starts from the specified index. Parameter: CH - A character. FromIndex - Index to start the search. Return the index of a character greater than or equal to fromIndex for the first occurrence in the character sequence represented by this object; If the character does not occur, -1 is returned.Copy the code
This method is called indexOf, which can be one or two parameters, depending on your needs. Note that this method returns the index of the first time the character was found; if not, -1 is returned. For arguments, you can pass in a string, return the index of the first character of the string if the string exists, and -1 if it does not.
The above is a string comparison and obtain related methods, if there are errors, welcome to dig friends to correct. Next time, we’ll look at some more methods of the String class.