How to split a string in Java

0 votes

I have with me a string with the numbers, "004-034556", that I want to split this into two separate strings:
 

string1="004"; string2="034556";

Which would state that the first string will contain the characters before '-', and the second string will contain the characters after '-'. I also want to know if the string has '-' in it. If it does not have it included, then I will throw an exception but I need help in knowing how to do this?

Feb 9, 2022 in Java by Soham
• 9,710 points
741 views

1 answer to this question.

0 votes

By using the appropriate method, this can be done as shown below:

String#split().
String string = "004-034556"; 
String[] parts = string.split("-"); 
String part1 = parts[0]; // 004 
String part2 = parts[1]; // 034556

After this step, if you wish to split lets say a period/dot . which means "any character" in regex, uses either the backslash \ to escape the individual special character like a split("\\."), or it would be using the character class [] to represent literal character(s) like so split("[.]") to escape the entire string like so:- split(Pattern.quote(".")).

String[] parts = string.split(Pattern.quote(".")); // Split on period.

To test it prior, if the string contains certain character(s), just use String#contains().
 

if (string.contains("-")) { 
    // Split it. 
} else { 
    throw new IllegalArgumentException("String " + string + " does not contain -");
}

Please remember that this will not take an expression that is regular and for that reason, we need to use String#matches() instead. You will have to make use of the positive look around in case you wish to get the split character back in resulting parts. To ensure that the left side is where you want your split character to end up, then commence by using a positive lookbehind prefixing ?<= group on the pattern.
 

String string = "004-034556"; 
String[] parts = string.split("(?<=-)"); 
String part1 = parts[0]; // 004- 
String part2 = parts[1]; // 034556

In case you want to have the split character to end up on the right hand side, then use positive lookahead by prefixing ?= group on the pattern.

String string = "004-034556"; 
String[] parts = string.split("(?=-)"); 
String part1 = parts[0]; // 004 
String part2 = parts[1]; // -034556

If you would want to limit the number of the resulting parts, then you could supply the given desired number as the 2nd argument of the split() method.
 

String string = "004-034556-42"; 
String[] parts = string.split("-", 2); 
String part1 = parts[0]; // 004 
String part2 = parts[1]; // 034556-42

Hope this helps!

Enroll for online Java course and become the expert.

Thanks!

answered Feb 9, 2022 by Rahul
• 9,680 points

edited Jul 6, 2023 by Khan Sarfaraz

Related Questions In Java

0 votes
0 answers

How to split a string in Java

I have a string, "004-034556", that I want ...READ MORE

Apr 21, 2022 in Java by Rahul
• 3,380 points
568 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,311 views
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 17, 2018 in Java by samarth295
• 2,220 points
1,569 views
0 votes
2 answers

How to find out a single character appears in String or not in Java?

You can use string.indexOf('s'). If the 's' is present in string, ...READ MORE

answered Aug 7, 2018 in Java by Sushmita
• 6,920 points
5,717 views
0 votes
1 answer

How can two strings be concatenated in java?

You can concatenate Strings using the + operator: System.out.println("Your number ...READ MORE

answered Jun 6, 2018 in Java by Daisy
• 8,140 points
823 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,920 points
5,656 views
0 votes
1 answer

Why is char[] preferred over a string?

Strings are immutable. That means once you've ...READ MORE

answered Jun 14, 2018 in Java by scarlett
• 1,290 points
666 views
0 votes
1 answer

Can quotes be added in a java string.how?

In Java, you can escape quotes with \: String ...READ MORE

answered Jun 14, 2018 in Java by Akrati
• 960 points
693 views
0 votes
1 answer
0 votes
1 answer

Failed to load resource: the server responded with a status of 404 (not found)

In order to avoid an error while ...READ MORE

answered Feb 8, 2022 in Java by Rahul
• 9,680 points
2,095 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP