Split in Java Example

(Last Updated On: August 11, 2016)

Split Function:

 
You can split a string using regex(regular expression) or by comma or anything you want.

Here example given to split a string by comma.

String Split by Comma Java Program:

package in.javadomain;

import java.util.ArrayList;

public class CommaSplit {

	public static void main(String[] args) {
		String links = "http://www.google.com,http://www.facebook.com,http://www.youtube.com,http://www.ebay.com";
		ArrayList linkList = new ArrayList();

		// Splitting by comma and storing in string array
		String[] linkSplit = links.split(",");
		for (int i = 0; i < linkSplit.length; i++) {
			linkList.add(linkSplit[i]);
		}

		// Printing ArrayList values
		for (Object link : linkList) {
			System.out.println(link);
		}

	}

}

Output:

http://www.google.com
http://www.facebook.com
http://www.youtube.com
http://www.ebay.com

Recommended Books:

196 total views, 1 views today

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *