package javasplitestring; import java.util.Scanner; /** * @web http://java-buddy.blogspot.com/ */ public class JavaSpliteString { public static void main(String[] args) { String original; Scanner scanner = new Scanner(System.in); System.out.println("Enter a string:"); original = scanner.nextLine(); //split String to array splited with whitespace regex String[] splittedStringArray = original.split("\\s+"); System.out.println("length = " + splittedStringArray.length); for(String s : splittedStringArray){ System.out.println(s); } //Join the String array to a String using StringBuilder StringBuilder stringBuilder = new StringBuilder(); for(String s : splittedStringArray){ stringBuilder.append(s); } System.out.println(stringBuilder); } }
Tuesday, March 31, 2015
Split String to array with whitespace
Example to split String to String array, by whitespace. And also join String array to String.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment