There are various ways in which we can split a String in Java, but the most efficient way is making use of regular expressions.
If you have a String that you want to split on dot (.), then you can write you code as follows,
package org.code2care.example;
public class JavaSplitOnDotStringExample {
public static void main(String[] args) {
String sentence = "This is sentence one. This is sentence two";
String[] splittedStringArray = sentence.split("\\.\\s");
for (String string : splittedStringArray) {
System.out.println(string);
}
}
}
Output:
We have made use of the split() function from the java.lang.String class
public String[] split(String regex)
Let's see another example with Pattern and Matcher classes from java.util.regex.
package org.code2care.example;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaSplitOnDotStringExample2 {
public static void main(String[] args) {
String sentence = "1.2.3.4.5.6.7.8";
Pattern pattern = Pattern.compile("(\\d+)");
Matcher matcher = pattern.matcher(sentence);
//Print them out
while (matcher.find()) {
String splittedString = matcher.group(1);
System.out.print(splittedString+"\t");
}
}
}
Output:
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Fix: python3: Library/Developer/CommandLineTools/usr/bin/python3: No module named notebook (jupyter) - Python
- How to Get File Size using Python in Bytes/KB/MB or GB - Python
- Eclipse : This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in - Eclipse
- Possible outages message Google Webmaster tool - Google
- The default username and password for RabbitMQ - HowTos
- How to stop and start a docker container - Docker
- Update All Outdated Modules/Packages using pip3 - PIP
- Chessboard with pieces using pure HTML and CSS - Html