Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Junilu Lacar
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Knute Snortum
• Tim Cooke
• Devaka Cooray
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Frits Walraven
• Ganesh Patekar

# Convert a string into an array of String

Greenhorn
Posts: 9
Hi,

my requirement is to convert a large string variable into an array of string
(i.e) assume that my string variable is
String tmp="abcdeabcdeabc";

I need to convert this into an array of string as mentioned below

String [] str = new String[5];
str[0]="abcde";
str[1]="abcde";
str[2]="abc";

Any help on this will be appreciated.

author and iconoclast
Posts: 24203
43
You could use a simple for-loop and the String.substring() method. The index of the loop would start at zero and go up by five; at each step you'd call substring() to make a five-character String (or less if there aren't five characters less.) You can compute the length of the String array before running the loop by dividing the String's length by 5 and rounding up.

Arun Moose
Greenhorn
Posts: 9
Yeah i've tried this. but the problem is at the last iteration of the for loop, when i try to make the 5 charecter string i'm getting an arrayoutofboundexception as the length of the string here is only 3 and not 5.

Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
OK, cool. So all you need to do is compute the length of the next string you can make. Let's say "i" is the index of the character at the beginning of the String. You'd like to use "i+5" as the end index, but that might be too far. When is it too far? When it's larger then the length of the String. So what you actually want for the end index is the smaller of "i+5" and "theString.length".

So the two arguments to substring() should be "i" and "the smaller of i+5 and theString.length".

Hmmm. Maybe there's a function that returns the smaller of two ints, somewhere. Maybe in the java.lang.Math class....

Arun Moose
Greenhorn
Posts: 9
I got it. here's the code

can this code be optimised in anyway???

[ November 21, 2008: Message edited by: Campbell Ritchie ]

Marshal
Posts: 65842
250
Please use the code tags; I have added them to your post so you can see how much better it looks.

Always make "optimisation" your lowest priority. Apart from using efficient algorithms, there is little you can do in a simple program which makes much difference.

One little stylistic point: please spread the code out. Leave spaces between tokens. Write 1 + 2 + 3 rather than 1+2+3. Like the code tags and indenting, that makes it much easier to read.

Beware of integer division. If the length of your String is 49, then length / 10 is 4, so you will go forward in steps of 4 and end up with your last substring 36 characters long.

 A teeny tiny vulgar attempt to get you to buy our stuff Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java