Home > Java > Javagetting Started > body text

How to intercept a string in java

王林
Release: 2020-10-20 09:42:46
Original
27645 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:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template