954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to split a string into array ?

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 :)

nomin-ginger
Newbie Poster
4 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
JeffGrigg
Posting Whiz in Training
218 posts since Aug 2011
Reputation Points: 192
Solved Threads: 28
 

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
JeroenvDijk
Newbie Poster
5 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
 

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

nomin-ginger
Newbie Poster
4 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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

nomin-ginger
Newbie Poster
4 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: