Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a String.strip () string

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "what is a String.strip () string". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Learn to use the strip (), stripLeading (), and stripTrailing () methods of the String class to remove unwanted spaces from the given string in Java 11.

String strip ()

Starting with Java 11, the String class contains three other methods that help eliminate extra spaces. Use the Character.isWhitespace (char) method to determine white space characters.

String strip ()-returns a string whose value is specified as a string and all left and right spaces are deleted.

String stripLeading ()-returns a string whose value is a string where all left whitespace has been deleted.

String stripTrailing ()-returns a string whose value is a string and removes all right whitespace.

Public class Main {public static void main (String [] args) {String str = "Hello World!"; System.out.println (str.strip ()); / / "Hello World!!" System.out.println (str.stripLeading ()); / / "Hello World!!" System.out.println (str.stripTrailing ()); / / "Hello World!"}} regular white space removal

Without using Java 11, you can use regular expressions to trim the spaces around a string.

Regular expression description ^ [\ t] + | [\ t] + $Delete leading space and trailing space ^ [\ t] + delete only leading space [\ t] + $delete only trailing space public class Main {public static void main (String [] args) {String str = "Hello World!!" System.out.println (str.replaceAll ("^ [\ t] + | [\ t] + $", ")); / /" Hello World!! " System.out.println (str.replaceAll ("^ [\ t] +", ")); / /" Hello World! " System.out.println (str.replaceAll ("[\ t] + $", ")); / /" Hello World! "}" what is the String.strip () string "ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report