RSS

Code to achieve split functionality in android

19 Jun

split function is already available in java.
for which we have to pass regularExpression and limit.
Same functionality i have tried in my way, which will give you exact result which u want.
Inputs:
strOriginal = pass the data here to split
strSeparator = pass the data which will split with the help of.
intMaxsize = pass the limit to get the number of resultant substrings by splitting the string.

private static String[] split(String strOriginal,String strSeparator,int intMaxsize)
	{
		Vector<String> nodes = new Vector<String>();
		//Parse the nodes into vector
		int index = strOriginal.indexOf(strSeparator);
		while(index>=0)
		{
			nodes.addElement(strOriginal.substring(0, index));
			strOriginal = strOriginal.substring(index+strSeparator.length());
			index = strOriginal.indexOf(strSeparator);
		}
		// Get the last node
		nodes.addElement( strOriginal );

		// Create splitted string array
		String[] result;
		result = new String[nodes.size()];

		if(nodes.size()>0)
		{
			for(int loop=0; loop<nodes.size(); loop++)
			{
				result[loop] = (String)nodes.elementAt(loop);
				//Avoid Memory load
				if(intMaxsize!=0)
				{
					if(loop==intMaxsize)
						break;
				}
			}
		}
		nodes=null;
		return result;
	}

Note: For more details
http://www.java-examples.com/java-string-split-example

Advertisements
 
Leave a comment

Posted by on June 19, 2012 in Android Source Code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: