java.lang Summary 2 (String)

String class

hashCode method

A method that returns a hash code for a character.

The hash code is calculated by the following formula,

String.hashCode = \sum^{n}_{k = 1} 31^{(l-k)}*s[k-1]  \text{(l = String.length())}

s [k] is the kth character of the index, and when the string is String.length () = 0, String.hashCode will be 0 ~~ (I don't care because java will do it for me. You don't have to do it) ~~.

lastIndexOf method

A method that returns the maximum value of the specified string, that is, the index number where the specified string appears last. The argument can also be specified in Unicode. You can also specify an index number to start the search and start from there.

startsWith method

Returns true if the specified string is in the search string as a prefix.

StarrtsWithSample.java


public class StarrtsWithSample {

	public static void main(String[] args) {
		String str = "Java String";
		
		System.out.println(str.startsWith("Java"));        //Java~
		System.out.println(str.startsWith(" Strin"));      //Strin~
		System.out.println(str.startsWith("Java Str"));    //Java Str~
		System.out.println(str.startsWith("ing"));         //ing~
		System.out.println(str.startsWith("J"));           //J~
	}
}

The execution result is

true
false
true
false
true

If there is a character string with the specified prefix, it will return true. In the above example, Java ~, Strin ~, Java Str ~, ing ~, and J ~ are searched as prefixes.

endsWith method

Contrary to the previous startsWith, this time it specifies a suffix and returns true if the string is in the search string.

For example, if you write something that searches for English words to see if there is ~ ing behind it,

EndsWithSample.java


public class EndsWithSample {

	public static void main(String[] args) {
		String[] strs = {"trying","boolean","string","building","having","catch","throw"};
		
		for(String str : strs){
			System.out.println("Search string: " + str);
			
			if(str.endsWith("ing")){
				System.out.println("There is ~ ing.");
			} else {
				System.out.println("There is no ~ ing.");
			}
		}
	}
}

The result is as follows.

Search string: trying
There is ~ ing.
Search string: boolean
There is no ~ ing.
Search string: string
There is ~ ing.
Search string: building
There is ~ ing.
Search string: having
There is ~ ing.
Search string: catch
There is no ~ ing.
Search string: throw
There is no ~ ing.

isEmpty method

A method that returns true when length () = 0.

split method

A method that splits by a specified string and returns the result as an array. It is also possible to specify how many times to divide.

The following is the one that divides the URL with /

SplitSample.java


public class SplitSample {

	public static void main(String[] args) {
		String str = "developers.google.com/analytics/devguides/reporting/mcf/v3/";
		
		for(String part : str.split("/")) {
			System.out.println(part);
		}
	}
}

Result is

developers.google.com
analytics
devguides
reporting
mcf
v3

become.

toCharArray method

A method that returns a character string (String type) as an array of Char.

For example

ToCharArraySample.java


public class ToCharArraySample {

	public static void main(String[] args) {
		String str = "Google";
		char[] char1 = str.toCharArray();
		
		for(char partChar : char1) {
			System.out.println(partChar);
		}
	}
}

When you execute

G
o
o
g
l
e

Is returned and stored in a char type array.

Recommended Posts

java.lang Summary 2 (String)
Summary
String
String puzzle
Object-oriented summary
Java string
ransack summary
String replacement
Liar String