Home  >  Article  >  Java  >  How to intercept a string in java

How to intercept a string in java

王林
王林Original
2020-10-20 09:42:4627646browse

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));

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));

Method 2: replace(), returns the StringBuilder

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

method Three: deleteCharAt(), returns StringBuilder

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

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!

Statement:
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