You'll need a loop, executed (length of string)/5 times
You can use the substring method in the String class to pick out 5 chars starting from an arbitrary start point...
Try doing this once by hand on a piece of paper to understand the exact sequence of steps, then convert that to Java code.
It's going to take about four lines of actual code, so its not hard. Just give it a try.
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
code:
System.out.println("string to array");
String s="abcdeabcdeabcdeqwertqwertqwert";
System.out.println(s);
int n= s.length();
System.out.println(n);
char[] array;
int count=n/5;
System.out.print(count);
array=s.toCharArray();
int j=0;
int tcount=0;
String sarray[]= new String[count];
for(int i=0;i<count;i++)
{
sarray[i]="";
}
for(int i=0;i<n;i++)
{
if (j==5){
j=0;
tcount++;
}
sarray[tcount]+=array[i];
j++;
}
for(int i=0;i<n;i++)
{
System.out.println(sarray[i]);
}
output
F:\studies\java\test>java sc
string to array
abcdeabcdeabcdeqwertqwertqwert
30
6abcde
abcde
abcde
qwert
qwert
qwert
sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3