Home arrow static arrow Java Programming [Archive] - Split String ??
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Split String ??
This topic has 9 replies on 1 page.

Posts:3
Registered: 8/6/04
Split String ??  
Aug 9, 2004 1:37 AM



 
Hi,
which would be best the way of cutting character strings without delimitors(ex : aaaaaaaabbbbbbbbccccccccdddddddd) in regular interval?

Thanks.
 

Posts:319
Registered: 11/8/00
Re: Split String ??  
Aug 9, 2004 1:52 AM (reply 1 of 9)



 
subString() ?

See the API docs: http://java.sun.com/j2se/1.4.2/docs/api/
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
 

Posts:3
Registered: 8/6/04
Re: Split String ??  
Aug 9, 2004 2:04 AM (reply 2 of 9)



 
Thank you for your answer but the method substring() obliges me has repeter the treatment several times according to the length of String. There would not be simpler?

For example( s.split(8) for divide the String in 8 parts...)
 

Posts:11,200
Registered: 7/22/99
Re: Split String ??  
Aug 9, 2004 2:57 AM (reply 3 of 9)



 
No, something like that is not needed very often so there's nothing in the standard api for it.
 

Posts:1
Registered: 8/9/04
Re: Split String ??  
Aug 9, 2004 4:29 AM (reply 4 of 9)



 
java.util.regexp
 

Posts:735
Registered: 11/07/01
Re: Split String ??  
Aug 9, 2004 5:50 AM (reply 5 of 9)



 
      String s = "aaaaaaaabbbbbbbbccccccccdddddddd"// '#' will be the delimiter       String[] a = s.replaceAll("(\\w)(?!\\1)", "$1#").split("#");       for (int j=0; j<a.length; j++)        System.out.println("" + (j+1) + ") '" + a[j] + "'");

the above results:
1) 'aaaaaaaa'
2) 'bbbbbbbb'
3) 'cccccccc'
4) 'dddddddd'
on target?
 

Posts:319
Registered: 11/8/00
Re: Split String ??  
Aug 9, 2004 5:56 AM (reply 6 of 9)



 
dekassegui's code will spliyt the input based on the character being the different not on the number of chars.
This will split on the number of chars.

        public List<String> split(String sToSplit, int inteval)	{		ArrayList<String> results = new ArrayList<String>(); 		for (int i = 0; i < sToSplit.length(); i = i + inteval)		{			if (i + inteval < sToSplit.length())			{				results.add(sToSplit.substring(i, i + inteval));			}			else			{				results.add(sToSplit.substring(i));			}		}		return results;	}
 

Posts:1,135
Registered: 1/16/04
Re: Split String ??  
Aug 9, 2004 6:12 AM (reply 7 of 9)



 
dekassegui's code will spliyt the input based on the
character being the different not on the number of
chars.
This will split on the number of chars.

</div>public List<String> split(String sToSplit, int inteval) {ArrayList<String> results = new ArrayList<String>(); for (int i = 0; i < sToSplit.length(); i = i + inteval) { if (i + inteval < sToSplit.length()){ results.add(sToSplit.substring(i, i + inteval));} else {results.add(sToSplit.substring(i)); } } return results; }


Nice, but what if the OP does not have J2SE 5.0 SDK beta2 ( aka 1.5.0)...
I am sure that all the "generics" tags will give the OP all kinds of compiler errors...
- MaxxDmg...

- ' He who never sleeps... '
 

Posts:3
Registered: 8/6/04
Re: Split String ??  
Aug 9, 2004 6:15 AM (reply 8 of 9)



 
Ok, thank you with all for your proposal.
I will test these Two dernieres solutions and will compare them to choose most effective.

Very large thank you of France and with soon.
 

Posts:319
Registered: 11/8/00
Re: Split String ??  
Aug 9, 2004 6:18 AM (reply 9 of 9)



 
a SDK 1.4 version

public List split(String sToSplit, int inteval)	{		ArrayList results = new ArrayList(); 		for (int i = 0; i < sToSplit.length(); i = i + inteval)		{			if (i + inteval < sToSplit.length())			{				results.add(sToSplit.substring(i, i + inteval));			}			else			{				results.add(sToSplit.substring(i));			}		}		return results;	}
 
This topic has 9 replies on 1 page.