Java Online Compiler
Example: Remove Leading/Trailing Spaces using strip() in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Remove Leading/Trailing Spaces using strip() public class Main { public static void main(String[] args) { // Step 1: Define an input string with various types of leading/trailing whitespace // Note: The first space is a regular space (U+0020), the last is a non-breaking space (U+00A0) String originalString = " Hello Java World\u00A0\u2005"; System.out.println("Original String: '" + originalString + "'"); System.out.println("Original String Length: " + originalString.length()); // Step 2: Use the strip() method to remove leading and trailing Unicode whitespace String strippedString = originalString.strip(); // Step 3: Print the result System.out.println("Stripped String: '" + strippedString + "'"); System.out.println("Stripped String Length: " + strippedString.length()); // For comparison, let's see what trim() does with the same string String trimmedString = originalString.trim(); System.out.println("Trimmed String (for comparison): '" + trimmedString + "'"); System.out.println("Trimmed String Length: " + trimmedString.length()); } }
Output
Clear
ADVERTISEMENTS