string.trim 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: string java

System.out.println("abc");
     String cde = "cde";
     System.out.println("abc" + cde);
     String c = "abc".substring(2,3);
     String d = cde.substring(1, 2);

Example 3: 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);
    }
}

Example 4: string java

char data[] = {'a', 'b', 'c'};
     String str = new String(data);

Tags:

Java Example