Answers for "split string in java using regex"

44

How to split a string in Java

String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
Posted by: Guest on February-01-2020
13

split method in java

public class SplitExample2 { 
    public static void main(String args[]) 
    { 
        String str = "My name is Chaitanya";
        //regular expression is a whitespace here 
        String[] arr = str.split(" "); 
  
        for (String s : arr) 
            System.out.println(s); 
    } 
}
Posted by: Guest on May-23-2020
-1

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 ); //group 0 is always the entire match   
  tokens.add(token);
}
//now iterate through 'tokens' and check whether you have a number or text
Posted by: Guest on June-24-2020

Code answers related to "split string in java using regex"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language