java split string with regex code example
Example 1: java split string
String yourString = "Hello/Test/World";
String[] strings = yourString.split("/" );
Output:
strings = [Hello, Test, World]
Example 2: java string split from input string
String line = scann.nextLine();
String[] info = line.split(" ");
for( String s : info) {
System.out.println(s);
}
Example 3: Regex split java
package com.tutorialspoint;
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str = "a d, m, i.n";
String delimiters = "\\s+|,\\s*|\\.\\s*";
String[] tokensVal = str.split(delimiters);
System.out.println("Count of tokens = " + tokensVal.length);
for(String token : tokensVal) {
System.out.print(token);
}
}
}
Example 4: splitting using regex java
Pattern p = Pattern.compile("(\\d+)|([a-zA-Z]+)");
Matcher m = p.matcher("810LN15");
List<String> tokens = new LinkedList<String>();
while(m.find())
{
String token = m.group( 1 );
tokens.add(token);
}