i have a very long string like this: TGTGCAACGTATATTCCAACGAAAAACCTGGAGAAGAAAAGAATAAGTAAAATGAACTAGAGGCTGATGGCACAGTAAACACAAATGCCTGAAGTCAAAATACATTCTTTATAAGCCCAAAGCG i want to convert it into array
but i wanna split it by length of 5 array elements:

array[0] = "TGTGC";
array[1] = "AACGT";
.....
array[n] = "AAGCG";

any idea? plz....... help

thanks :)

Recommended Answers

All 6 Replies

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.

Just an example:

String string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		int stringPart = 5;
		int arraySize = string.length() / 5;
		if (string.length() > stringPart * arraySize)
		{
			arraySize += 1;
		}
		String[] stringArray = new String[arraySize];
		for (int iterator = 0; (iterator * stringPart) < string.length(); iterator++)
		{
			int start = iterator * stringPart;
			int end = start + stringPart;
			if (end > string.length())
			{
				end = string.length();
			}
			stringArray[iterator] = string.substring(start, end);
		}
		int row = 1;
		for (String s : stringArray)
		{
			System.out.println(row++ + "" + ": " + s);
		}

will nicely result in:

1: abcde
2: fghij
3: klmno
4: pqrst
5: uvwxy
6: zABCD
7: EFGHI
8: JKLMN
9: OPQRS
10: TUVWX
11: YZ123
12: 45678
13: 90abc
14: defgh
15: ijklm
16: nopqr
17: stuvw
18: xyzAB
19: CDEFG
20: HIJKL
21: MNOPQ
22: RSTUV
23: WXYZ1
24: 23456
25: 7890

Thanks, your replies were helpful :) I managed to make something, well not exactly array, but looks almost same

Just an example:

String string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		int stringPart = 5;
		int arraySize = string.length() / 5;
		if (string.length() > stringPart * arraySize)
		{
			arraySize += 1;
		}
		String[] stringArray = new String[arraySize];
		for (int iterator = 0; (iterator * stringPart) < string.length(); iterator++)
		{
			int start = iterator * stringPart;
			int end = start + stringPart;
			if (end > string.length())
			{
				end = string.length();
			}
			stringArray[iterator] = string.substring(start, end);
		}
		int row = 1;
		for (String s : stringArray)
		{
			System.out.println(row++ + "" + ": " + s);
		}

will nicely result in:

1: abcde
2: fghij
3: klmno
4: pqrst
5: uvwxy
6: zABCD
7: EFGHI
8: JKLMN
9: OPQRS
10: TUVWX
11: YZ123
12: 45678
13: 90abc
14: defgh
15: ijklm
16: nopqr
17: stuvw
18: xyzAB
19: CDEFG
20: HIJKL
21: MNOPQ
22: RSTUV
23: WXYZ1
24: 23456
25: 7890

thanks a lot :D

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.