Home  >  Article  >  Java  >  Detailed explanation of String.trim() method examples in Java

Detailed explanation of String.trim() method examples in Java

零下一度
零下一度Original
2017-05-20 10:02:252599browse

Java String.trim() method usage example tutorial, this method returns a copy of the beginning and end of the string with white spaces removed or a string if it has no leading or trailing spaces.

Description

##java.lang.String.trim () method returns a copy of the string and ignores (removes) leading and trailing whitespace

The following is the declaration of the java.lang.String.trim() method

public String trim()

Return value


This method returns a copy of the leading and trailing white spaces of the string, or the string if it has no leading or trailing white spaces.

The following example demonstrates the use of java.lang.String.trim() method

包装com.yiibai;import java.lang。*;public class StringDemo {
 public static void main(String [] args){
 
   //带有前导和尾随空格的字符串
   String str =“This is YiiBai”;
   
   System.out.print(“Before trim =”);
   System.out.println(“..”+ str +“..”);
   
   //前导和尾随的空格被删除
   System.out.print(“After trim =”);
   System.out.println(“..”+ str.trim()+“..”);
 }}

Let us compile and run the above program, which will produce the following results:

之前修剪= ..这是YiiBai ..
修剪= ..这是YiiBai ..

[Related recommendations]


1.

Share examples of TrimHelper methods in Java

2.

Detailed explanation of usage examples of java trim

3.

How to remove spaces on both sides of a string using trim() in js

4.

The trim() function in Java

5.

The PHP function trim() that removes leading and trailing spaces and special characters from a string

The above is the detailed content of Detailed explanation of String.trim() method examples 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