DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Java - Splitta Una Stringa

02.26.2007
| 5377 views |
  • submit to reddit
        
	// Splitta una stringa
	private String[] splitString(String str, String delims)
	{
		if(str == null)
			return null;
		else if(str.equals("") || delims == null || delims.length() == 0)
			return new String[]{ str };
		
		String[] s;
	  	Vector v = new Vector();
		
	  	int pos = 0;
		int newpos = str.indexOf(delims, pos);;

		while(newpos != -1)
		{
			v.addElement(str.substring(pos, newpos));
			pos = newpos + delims.length();
			newpos = str.indexOf(delims, pos);
		}
		v.addElement(str.substring(pos));
		
		s = new String[v.size()];
		for(int i=0, cnt=s.length; i<cnt; i++)
			s[i] = (String) v.elementAt(i);
		
		return s;
	}
    

Comments

replied on Sat, 2008/02/02 - 6:34pm

Why not just use String.split()?

Snippets Manager replied on Thu, 2006/02/09 - 1:52pm

I use this function for Mobile Phone ^_^

Torsten Krall replied on Wed, 2007/02/14 - 5:08am

Why don't you use the class StringTokenizer?