此方法用于从字符串中去除尾随空格,即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#
相关用法
- Java Class forName(String, boolean, ClassLoader)用法及代码示例
- Java String repeat()用法及代码示例
- Java String strip()用法及代码示例
- Java String stripLeading()用法及代码示例
- Java String indent()用法及代码示例
- Java String lines()用法及代码示例
- Java Java.util.concurrent.RecursiveAction用法及代码示例
- Java Java.util.concurrent.RecursiveTask用法及代码示例
- Java Java.awt.image.RescaleOp用法及代码示例
- Java Java.util.concurrent.Phaser用法及代码示例
- Java Class getModule()用法及代码示例
- Java Class getPackage()用法及代码示例
- Java Class getPackageName()用法及代码示例
- Java Class getTypeParameters()用法及代码示例
- Java Class getDeclaredClasses()用法及代码示例
- Java Class getDeclaredMethods()用法及代码示例
注:本文由纯净天空筛选整理自kapilm180265ca大神的英文原创作品 String Class stripTrailing() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。