×

Java Programs

Java Practice

How to spilt string in substrings using String.split() in Java?

String.split() in Java: Here, we will learn how to split string in substrings separating by given delimiter in java?

Given string and delimiter and we have to separate the given string in substrings.

1) String [] split (String delimiter)

This function returns array of strings, separated by delimiter (which is a string, passing as a delimiter).

2) String [] split (String delimiter, int max)

Here,
String[] - array of strings (substrings separated by given delimiter).
String delimiter - string as delimiter to split string.
int max - controls the number of substrings separated through given delimiter.

Consider the program:

public class JavaStringSplitPrg
{
	public static void main(String []args)
	{
		try
		{
			String str1="www.includehelp.com";
			int loop;

			String [] arrStr1;

			/*split string by delimiter ., to do this
			* you have to provide \\. */
			arrStr1 = str1.split("\\.");
			// print substrings
			for(loop=0;loop < arrStr1.length; loop++)
			{
				System.out.println(arrStr1[loop]);
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

Output

www
includehelp
com

Java String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.