trim string java code example

Example 1: Java string trim() method

// example on java String trim() method
public class StringTrimJava
{
   public static void main(String[] args)
   {
      String str = "     welcome to helloworld java!        ";
      System.out.println(str);
      System.out.println(str.trim());
   }
}

Example 2: trim and split java

package com.zetcode;

import java.util.Arrays;

public class SplitStringTrimEx {

    public static void main(String[] args) {

        var input = " wood, falcon\t, sky, forest\n";

        var output = input.trim().split("\\s*,\\s*");

        Arrays.stream(output).forEach(System.out::println);
    }
}

Tags:

Java Example