Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert a string into an array of String

 
Arun Moose
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. here's the code



can this code be optimised in anyway???

[edit]Add code tags. CR[/edit]
[ November 21, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic