Java example to convert string into string array using String.split()
method and using java.util.regex.Pattern
class.
Quick Reference:
//1. String to String[] String blogName = "how to do in java"; String[] words = null; // Method 1 :: using String.split() method words = blogName.split(" "); //[how, to, do, in, java] // Method 2 :: using Pattern.split() method Pattern pattern = Pattern.compile(" "); words = pattern.split(blogName); //[how, to, do, in, java] //2. String[] to String String newName = String.join(" ", words); //"how to do in java"
1) Convert String to String[]
1.1) String.split()
Use split()
method to split string into tokens by passing a delimiter (or regex) as method argument.
import java.util.Arrays; public class Main { public static void main(String[] args) { String names = "alex,brian,charles,david"; String[] namesArray = null; //Split string with comma namesArray = names.split(","); //Verify array content System.out.println(Arrays.toString(namesArray)); } } Output: [alex, brian, charles, david]
1.2) Pattern.split()
In Java, Pattern
is the compiled representation of a regular expression. Use Pattern.split()
method to convert string to string array, and using pattern as delimiter.
import java.util.Arrays; public class Main { public static void main(String[] args) { String names = "alex,brian,charles,david"; String[] namesArray = null; //Split string with comma Pattern pattern = Pattern.compile(","); namesArray = pattern.split( names ); //Verify array content System.out.println(Arrays.toString(namesArray)); } } Output: [alex, brian, charles, david]
2) String[] to String
Use String.join()
method to create string from String array. You need to pass two method arguments i.e.
- delimiter – the delimiter that separates each element
- array elements – the elements to join together
It will then return a new string that is composed of the ‘array elements’ separated by the ‘delimiter’.
public class Main { public static void main(String[] args) { String[] tokens = {"How","To","Do","In","Java"}; String blogName1 = String.join("", tokens); //without space String blogName2 = String.join(" ", tokens); //with one space String blogName3 = String.join("-", tokens); //with hyphen //Verify string System.out.println(blogName1); System.out.println(blogName2); System.out.println(blogName3); } } Output: HowToDoInJava How To Do In Java How-To-Do-In-Java
Drop me your questions in comments section.
Happy Learning !!
String.split() Java Doc
String.join() Java Doc
Pattern Java Doc
Arrays.toString() Java Doc
Leave a Reply