The substring method in Java to intercept a string: [public String substring(int beginIndex)] starts from the index beginIndex and ends at the end of the entire string.
The substring method in Java to intercept a string:
1, public String substring(int beginIndex)
The string intercepted by this method starts from the index beginIndex and ends at the end of the entire string, for example: String String s = "abcdef";
Call s .substring(2) means intercepting from index 2 of the string to the end of the entire string. The intercepted string is cdef
2, public String substring(int beginIndex, int endIndex)
The string intercepted by this method starts from beginIndex and ends at endIndex - 1 of the string index. That is, the intercepted string does not include the characters corresponding to the endIndex index, so the maximum value of endIndex is the entire string. Length, so when using this method, you need to pay special attention to the problem of string interception and out-of-bounds problems.
The following is the specific code:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); System.out.println(s.substring(0, 2)); System.out.println(s.substring(2)); sc.close(); } }
Input from the console: saahdfasgfdga
Related learning recommendations: java basic tutorial
The above is the detailed content of How to intercept a string using substring in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software