Home arrow static arrow Java Programming [Archive] - string to string[] without looping
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - string to string[] without looping
This topic has 18 replies on 2 pages.    1 | 2 | Next »

Posts:158
Registered: 8/21/02
string to string[] without looping  
Aug 6, 2004 1:13 PM



 
Hi Guys,
Please help me.
Is there a way to convert String to String[] in one line ie without looping. in java 1.3.0

I have a string eg String str = "This is a book"
I want to conver it to an array String[] stra so it is
stra[0]="This"
stra[0]="is"
stra[0]="a"
stra[0]="book"

Thanks
Java lover
 

Posts:24,036
Registered: 2/3/03
Re: string to string[] without looping  
Aug 6, 2004 1:18 PM (reply 1 of 18)



 
String[] stra = str.split(" ");
It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of [url=http://java.sun.com/reference/api/index.html]documentation[/url] that you can even [url=http://java.sun.com/docs/index.html]download[/url] for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.

http://java.sun.com/reference/api/index.html
http://java.sun.com/j2se/1.4.2/docs/api/
 

Posts:31,095
Registered: 4/30/99
Re: string to string[] without looping  
Aug 6, 2004 1:21 PM (reply 2 of 18)



 
Sure:
StringTokenizer st = new StringTokenizer(str, " "); String[] stra = new String[st.countTokens()]; int i = 0; while (st.hasMoreTokens()) {stra[i] = st.nextToken(); i++};
 

Posts:31,095
Registered: 4/30/99
Re: string to string[] without looping  
Aug 6, 2004 1:22 PM (reply 3 of 18)



 
String[] stra = str.split(" ");
doesn't work in 1.3.0 as the OP requested.
 

Posts:158
Registered: 8/21/02
Re: string to string[] without looping  
Aug 6, 2004 1:30 PM (reply 4 of 18)



 
Thanks for resonse guys but I know about 1.4 string.split method and I also know about stringtokenizer.
So does it mean there is no way to do it?
 

Posts:6,750
Registered: 1/25/04
Re: string to string[] without looping  
Aug 6, 2004 1:32 PM (reply 5 of 18)



 
I'm left wondering why you would want to...
 

Posts:826
Registered: 7/1/03
Re: string to string[] without looping  
Aug 6, 2004 1:44 PM (reply 6 of 18)



 
If I was to be really completely literal about the OP, then this would do it:
stra[0] = str.substring(str.lastIndexOf(" ") + 1);

I mean, he did want stra[0] to be "book", right?
/k1
 

Posts:158
Registered: 8/21/02
Re: string to string[] without looping  
Aug 6, 2004 2:05 PM (reply 7 of 18)



 
Sorry komone my typing mistake.

Is there a way to convert String to String[] in one line ie without looping. in java 1.3.0

I have a string eg String str = "This is a book"
I want to conver it to an array String[] stra so it is
stra[0]="This"
stra[1]="is"
stra[2]="a"
stra[3]="book"

not index 0 for all stra
 

Posts:158
Registered: 8/21/02
Re: string to string[] without looping  
Aug 6, 2004 2:06 PM (reply 8 of 18)



 
tasch_
I wanted to do it because of performance issue. my application has be good performance when it processes huge amount of data.
 

Posts:8,813
Registered: 10/4/00
Re: string to string[] without looping  
Aug 6, 2004 2:14 PM (reply 9 of 18)



 
Here's one statement, but it's going to be slower than several statements
import java.util.StringTokenizer;public class Test3 {  public static void main(String[] args) {    String str = "This is a book";    String[] stra;    for (Object[] obj = {new StringTokenizer(str, " "),stra=new String[(new StringTokenizer(str, " ").countTokens())],new int[] {0}}; ((StringTokenizer)obj[0]).hasMoreTokens(); ((String[])obj[1])[((int[])obj[2])[0]++]=((StringTokenizer)obj[0]).nextToken()) {}    for (int i=0; i<stra.length; i++) System.out.println("stra["+i+"]="+stra[i]);  }}
 

Posts:349
Registered: 1/8/04
Re: string to string[] without looping  
Aug 6, 2004 2:17 PM (reply 10 of 18)



 
My C instructor a couple years back told our class about a former co-worker who deleted all the blank space in company source code to save disk space (back when disk space was an issue).

Smaller source code has no bearing on program speed, only the amount of disk space you take up on your development machine.
 

Posts:8,813
Registered: 10/4/00
Re: string to string[] without looping  
Aug 6, 2004 2:19 PM (reply 11 of 18)



 
Smaller source code has no bearing on program speed,
only the amount of disk space you take up on your
development machine.

If you use a smaller font, you can get both better speed and less space.
 

Posts:37,103
Registered: 3/30/99
Re: string to string[] without looping  
Aug 6, 2004 2:23 PM (reply 12 of 18)



 
tasch_
I wanted to do it because of performance issue. my
application has be good performance when it processes
huge amount of data.

So, you don't want to loop? If you had 1.4 with split, do you think using split() would be faster than looping? What do you think split() does? If you're breaking a String up into pieces, you're going to loop. Just because the code that does that is in an API that you call rather than the code you personally write doesn't make the API any faster.

Split() might be faster than a loop you'd right for one of two reasons: 1) if it were a native method (using JNI) with the implementation in C/C++, or 2) if you use a very inefficient algorithm for your looping or scanning or splitting.

More to the point though: How much data will you have? How fast does it have to process this data? How do you know that whatever multi-lined loop you might write is too slow? Don't go looking for funky one-line solutions because you think they might perform better without understanding your requirements and having a good reason to believe your method will be too slow. In particular, in this case it's trivial to write a loop to do the processing and run it against some sample data to get an idea of the performance you'll get. Don't optimize until you know it will actually be worth it.
 

Posts:349
Registered: 1/8/04
Re: string to string[] without looping  
Aug 6, 2004 2:29 PM (reply 13 of 18)



 
Smaller source code has no bearing on program speed,
only the amount of disk space you take up on your
development machine.

If you use a smaller font, you can get both better
speed and less space.

I forgot that rule. Fonts definitely matter more than the amount of code. Also, use red text for the code you need really optimized. Use blue or green for wait() calls.

By the way, bbritta, has Robo asked you out yet? He seemed really interested.
 

Posts:10,967
Registered: 4/7/01
Re: string to string[] without looping  
Aug 6, 2004 2:33 PM (reply 14 of 18)



 
tasch_
I wanted to do it because of performance issue. my
application has be good performance when it processes
huge amount of data.

What percentage of the application's time is being taken up by this function?
 
This topic has 18 replies on 2 pages.    1 | 2 | Next »