Home > Java > Javagetting Started > How to intercept a string in java

How to intercept a string in java

王林
Release: 2020-10-20 09:42:46
Original
27738 people have browsed it

Java method of intercepting a string: You can use the substring() function, such as [s.substring(0,s.length() - 1)], which means removing the last character in the string.

How to intercept a string in java

String removes the last character

(Recommended tutorial: java course)

String s = "name=Marydon&sex=男&age=18&";
System.out.println("String去除最后一个字符:" + s.substring(0,s.length() - 1));
Copy after login

StringBuilder Remove the last character

Method 1: substring(), returns the String type. It is recommended to use

StringBuilder sb = new StringBuilder("name=Marydon&sex=男&age=18&");
System.out.println("StringBuilder去除最后一个字符》方式一:" + sb.substring(0,sb.length() - 1));
Copy after login

Method 2: replace(), returns the StringBuilder

System.out.println("StringBuilder去除最后一个字符》方式二:"+sb.replace(sb.length() - 1,sb.length(),""));
Copy after login

method Three: deleteCharAt(), returns StringBuilder

System.out.println("StringBuilder去除最后一个字符》方式三:" + sb.deleteCharAt(sb.length() - 1));
Copy after login

Related recommendations:Getting Started with Java

The above is the detailed content of How to intercept a string in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template