dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jul 8, 2021

How to Split Strings in Java

Splitting strings is fairly simple in Java, as it is in most languages. This is thanks, in part, to the split() method. Here, we will split a string by its index value using the argument parameter. Learn how to split strings in Java with the code example below:

*/

public class StringSplit {
	
	static int delimiterIndex = -1;
	String delimiter = "#";
	
	public static void main(String[] args) 
	{  
		delimiterIndex = Integer.parseInt(args[0]);
		StringSplit stringSplit = new StringSplit();
		stringSplit.proceed(delimiterIndex);
	}

	private void proceed(int delimiterIndex)  
	{
		String stringToBeSplit = "the#world#is#wonderful";
        String[] splitStringArr = stringToBeSplit.split(delimiter,delimiterIndex);
  
        for (String splitString : splitStringArr)
            System.out.println(splitString);
		
	}
}

/*

You can expect the following output to the console when you run the Java code above:

[root@mypc]# java StringSplit 0
the
world
is
wonderful

[root@mypc]# java StringSplit 3
the
world
is#wonderful
MS Sridhar
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date