[Java] How to use substring to cut out a character string

Introduction

We participated in our Advent Calendar December 19th. This will be the first post of the article, so I hope you can see it with warm eyes.

This time, I will describe the substring method used when cutting out a character string in Java as a memo for myself.

What is substring

substring is a method to cut out a part of the character string and get it as a new character string.

When specifying only the start position

Syntax: " Target string.substring (start position); "


String target = target.substring(int beginindex); 

Cuts out the character on the right side from the specified index number.

Example


String target = "Thank you";
target = target.substring(4);

System.out.println(target);

result


Please

All the characters on the right side are cut out from the specified index number and acquired as a new character string. (* Index number starts from 0.)

When specifying the end position

Syntax: " Target string.substring (start position, end position); "


String target = target.substring(int beginIndex, int endIndex); 

Cuts out the characters at the specified end position from the specified start position.

Example


String target = "Thank you";
target = target.substring(4,7);

System.out.println(target);

result


Please

The character on the right side of the index number of the start position is cut out to just before the end position and acquired as a new character string. Note that the start position character is included, but the end position is not included and is to the front.

Recommended Posts

[Java] How to use substring to cut out a character string
[Java] How to use substring to cut out a part of a character string
[Java] How to cut out a character string character by character
[Java] How to erase a specific character from a character string
[Java] How to convert a character string from String type to byte type
[Android] How to convert a character string to resourceId
[Java] How to use Map
[Java] How to use Map
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
Cut out a Ruby string
How to use Java HttpClient (Get)
How to make a Java container
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
How to store a string from ArrayList to String in Java (Personal)
[Java] How to create a folder
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
[Java] Cut out a part of the character string with Matcher and regular expression
How to make a Java array
How to use Java lambda expressions
[Java] How to use Math class
A story about misunderstanding how to use java scanner (memo)
How to use Java enum type
How to check for the contents of a java fixed-length string
[Java] Divide a character string by a specified character
How to make a Java calendar Summary
A memorandum on how to use Eclipse
Multilingual Locale in Java How to use Locale
[Java] How to use the HashMap class
[Easy-to-understand explanation! ] How to use Java instance
[Introduction to Java] How to write a Java program
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to make a Discord bot (Java)
How to output Java string to console screen
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to print a Java Word document
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
How to use substring and substr methods
[Java] How to use the Calendar class
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation