Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Convert String to String[] in Java

Java examples to convert a string into string array using String.split() and java.util.regex.Pattern.slipt() methods.

Lokesh Gupta

February 22, 2023

Java Array
Java Array
Java 10

Java examples to convert a string into string array using String.split() and java.util.regex.Pattern.slipt() methods.

String blogName = "how to do in java";

String[] words = blogName.split(" ");				//[how, to, do, in, java]

Pattern pattern = Pattern.compile(" ");
String[] words = pattern.split(blogName);			//[how, to, do, in, java]

1. String -> String[]

1.1. Using String.split()

Use split() method to split a string into tokens by passing a delimiter (or regex) as method argument.

String names = "alex,brian,charles,david";

String[] namesArray = names.split(",");	//[alex, brian, charles, david]

1.2. Using 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 the pattern as the delimiter.

String names = "alex,brian,charles,david";

Pattern pattern = Pattern.compile(",");
String[] namesArray = pattern.split( names );	//[alex, brian, charles, david]

2. String[] -> String

Use String.join() method to create a 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

The join() will then return a new string that is composed of the ‘array elements’ separated by the ‘delimiter’.

String[] tokens = {"How","To","Do","In","Java"};

String blogName1 = String.join("", tokens);		//HowToDoInJava

String blogName2 = String.join(" ", tokens);	//How To Do In Java

String blogName3 = String.join("-", tokens);	//How-To-Do-In-Java

Drop me your questions in the comments section.

Happy Learning !!

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Java Array

  • Array Print
  • Array Copy
  • Array Clone
  • Array Union
  • Array Intersection
  • Remove Duplicates
  • String to String[]
  • String to byte[]

Table of Contents

  • 1. String -> String[]
    • 1.1. Using String.split()
    • 1.2. Using Pattern.split()
  • 2. String[] -> String
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

How to Create Subarray in Java

Next

Sorting Arrays in Java

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap