Home >Java >Javagetting Started >How to intercept a string in java
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.
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!