[JAVA] Various methods of the String class

Introduction

I am studying for the Java SE 8 Silver exam, and I would like to summarize what I learned during that time.

This time, I will organize various methods of the String class.

(I regret that there are many articles on character string manipulation ... sweat)

Method type

I have summarized the main methods that appear in the exam.

Method name Description
char charAt(int i) Returns the character at position i.
String concat(String str) Add a string
boolean endsWith(String suffix) Returns true if the string ends with the specified string, false if not.
boolean equalsIgnoreCase(String str) Compare strings. Insensitive to case.
int indexOf(int ch) Returns the position where the value entered in the actual argument first appears.
int lastIndexOf(int ch) Returns the position where the value entered in the actual argument appears last.
int length() Returns the number of characters in the string.
String replace(char o, char n) Returns the result of replacing the character o in the string with the character n.
boolean startsWith(String prefix) Returns true if the string starts with the value you put in the actual argument.
String substring(int i) 0 ~Returns a substring up to the value entered in the actual argument. (You can specify the start point.)
String toLowerCase() Convert uppercase to lowercase
String toUpperCase() Convert lowercase letters to uppercase
String toString() Returns the string held by the object.
String trim() Remove whitespace. (However, full-width space symbols are not removed)

Operation check

I'll delve into the methods that I find difficult to understand.

-Substring () method

Regarding the method of extracting the character string, if you do not pay attention to how to specify the range, the output will not be as expected.

Below is the sample code.

Main.java


public class Main {

	public static void main(String[] args) {

		String str = "There was a pig kimchi";

		//Explanation ①
		System.out.println(str.substring(0, 5));

	}

}


Output result

Main.java


Pig kimchi

Analysis ① Only "Pig Kimchi" is taken out, but when taking out ***, there must be 0 before the first character ***

Please be careful. It can be taken out by showing the range from 0 to 5.

Buta Kimchi is Ah, Ma.
 ↑    ↑    ↑    ↑    ↑    ↑    . . . .
 0    1   2   3   4   5

Another thing I want to organize is the trim () method.

・ Trim () method

It is a method that removes spaces before and after the character string.

It also removes tab characters such as \ t, \ n or \ r, and newlines.

Please note that it does not remove double-byte spaces and does not remove whitespace in the string.

To take the exam

There are many problems that can be understood by solving Java Silver problems.

Even though I called a convenient method of the String class and fetched the value

Since it is not assigned, there are many nasty problems such as the output is the same as before calling the method.

Therefore, I would like to take on the challenge with plenty of time when taking the exam.

Recommended Posts

Various methods of the String class
Various methods of Java String class
String class methods
super doesn't just call the methods of the parent class
How to mock some methods of the class under test
I read the source of String
I tried to summarize the methods of Java String and StringBuilder
Item 63: Beware the performance of string concatenation
[Java] How to get to the front of a specific string using the String class
[Ruby] Summary of class definitions. Master the basics.
The story of low-level string comparison in Java
[Delete the first letter of the character string] Ruby
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
About next () and nextLine () of the Scanner class
We have collected various implementation methods of singleton.
First touch of the Files class (or Java 8)
Use of Date class
Count the number of occurrences of a string in Ruby
About the StringBuilder class
The world of clara-rules (2)
Return the execution result of Service class in ServiceResponse class
Get the name of the test case in the JUnit test class
Judgment of the calendar
The world of clara-rules (1)
The world of clara-rules (3)
About Java String class
The world of clara-rules (5)
The idea of quicksort
Output of the day of the week using Ruby's Date class
[Ruby] Class nesting, inheritance, and the basics of self
[Java] Handling of character strings (String class and StringBuilder class)
Summarize the additional elements of the Optional class in Java 9
Various Ruby string operations
The idea of jQuery
About truncation by the number of bytes of String on Android
Put the file in the properties of string in spring xml configuration
When using the constructor of Java's Date class, the date advances by 1900.
The nth and n + 1st characters of a Ruby string
[Java] Try editing the elements of the Json string using the library
[Ruby] Display today's day of the week using Date class
I compared the build times of various Dockerfiles in Rust
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
Basic methods of Ruby hashes
About the handling of Null
Docker monitoring-explaining the basics of basics-
Basic methods of Ruby arrays
[GCD] Basics of DispatchQueue class
Java inflexible String class substring
[Ruby] Various types of each
About the File :: Stat class
The play of instantiating java.lang.Void
Example of using abstract class
What is the BufferedReader class?
[Java] Comparator of Collection class
Trap of asList of Arrays class
The basics of Swift's TableView
Median of the three values
What are Ruby class methods?
Summary of Java Math class