package string; public class Split { public static void main(String[] args) { String input=" value1 value2 value3 "; // need to do trim() first to remove leading spaces String[] fields=input.split("\\s+"); for(int i=0; i<fields.length; i++) { System.out.println(i+" #"+fields[i]+"#"); } } } /** output 0 ## 1 #value1# 2 #value2# 3 #value3# **/