

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to split and join a string
To split and join a string in Java, use the split() and join() method as in the below example −
Example
public class Demo{ public static void main(String args[]){ String my_str = "This_is_a_sample"; String[] split_str = my_str.split("_", 4); System.out.println("The split string is:"); for (String every_Str : split_str) System.out.println(every_Str); String joined_str = String.join("_", "This", "is", "a", "sample"); System.out.println("The joined string is:"); System.out.println(joined_str); } }
Output
The split string is: This is a sample The joined string is: This_is_a_sample
A class named Demo contains the main function. Here a String object is defined and it is split based on the ‘_’ value upto the last word. A ‘for’ loop is iterated over and the string is split based on the ‘_’ value. Again, the string is joined using the ‘join’ function. Relevant messages are displayed on the console.
- Related Questions & Answers
- C# program to split and join a string
- Python program to split and join a string?
- Python program to split a string and join with comma
- Java Program to split a string with dot
- Java Program to split a string using Regular Expression
- Java regex program to split a string at every space and punctuation.
- Split a string in Java
- Java StringTokenizer and String Split Example.
- How to split a string in Java?
- C# Program to split a string on spaces
- Java regex program to split a string with line endings as delimiter
- Java Program to Join Two Lists
- Java String split() method example.
- C# program to join words into a string in C#
- Split a string around a particular match in Java
Advertisements