當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java String stripTrailing()用法及代碼示例


此方法用於從字符串中去除尾隨空格,即stripTrailing()方法僅在字符串末尾刪除所有空格。

例:

Input:

String name = "   kapil   ";
System.out.println("#" + name + "#);
System.out.println("#" + name.stripLeading());


Output:

#   kapil   #
#   kapil# // trailing whitespaces are removed 

用法:

public String stripTrailing()

參數:它不接受任何參數。

返回:刪除字符串末尾的所有空格後的字符串。



Note:Like the stripTrailing() method in java, we have strip() (removes leading and trailing whitespace) and stripLeading() (removes the only leading whitespace). 

Java

// Java program to demonstrate the usage of 
// stripTrailing() method in comparison to  
// other methods 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // creating a string 
        String str = "  Hello,  World "; 
  
        // print the string without any function 
        System.out.println("String is"); 
        System.out.println("#" + str + "#"); 
        System.out.println(); 
  
        // using strip() method 
        System.out.println("Using strip()"); 
        System.out.println("#" + str.strip() + "#"); 
        System.out.println(); 
  
        // using stripLeading() method 
        System.out.println("Using stripLeading()"); 
        System.out.println("#" + str.stripLeading() + "#"); 
        System.out.println(); 
  
        // using stripTrailing() method 
        System.out.println("Using stripTrailing()"); 
        System.out.println("#" + str.stripTrailing() + "#"); 
    } 
}
輸出
String is
#  Hello,  World #

Using strip()
#Hello,  World#

Using stripLeading()
#Hello,  World #

Using stripTrailing()
#  Hello,  World#

相關用法


注:本文由純淨天空篩選整理自kapilm180265ca大神的英文原創作品 String Class stripTrailing() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。