Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Convert a string into an array of String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert a string into an array of String " Watch "Convert a string into an array of String " New topic
Author

Convert a string into an array of String

Arun Moose
Greenhorn

Joined: Oct 29, 2008
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Arun Moose
Greenhorn

Joined: Oct 29, 2008
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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Oct 29, 2008
Posts: 9
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

Joined: Oct 13, 2005
Posts: 38075
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert a string into an array of String
 
Similar Threads
Convert generic ArrayList to array of specific type
is "String[] str = (String[])ObjArray"" Right?
Passing a String reference to a method.
Question from Just Java 2
convert String in to String array ???