DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2003
    Posts
    12

    copying a string into array of string

    Hello,
    I want to copy a string into array of strings. I used system.copyarray(...),but it only works for the same type of array. Now i have a string containing a lot of words and i want to assigne each word to an elemnt of the stringarray.

    for example: i have the string " i am lucky but i am not";

    String[] words = new words[1000];

    String str ;

    the question is how can i copy each wod in the string into the array.

    the outpu should be :

    words[0] = I
    words[1] = am
    words[2] =lucky
    ........

    help..........please

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    Use a StringTokenizer to move through each word, putting it into the array.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Oct 2003
    Posts
    12

    hello again

    Could you please show us how to use the string tokenizer in an example. i have tried many times but
    it tdid not work propely .

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    There's an example on the API:

    http://java.sun.com/j2se/1.3/docs/ap...Tokenizer.html

    To change the delmitor, use the constructor:

    http://java.sun.com/j2se/1.3/docs/ap...tringTokenizer(java.lang.String,%20java.lang.String)

    If you still have problems, post your code and the error messages you're getting.
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Nov 2003
    Posts
    1

    Aternate method

    Hi! I'm new to this forum but I saw your question and I felt I had something to add.

    Using Java SDK from version 1.4 you can use the java.lang.String.split() method.
    This method offers a very convinient solution to your problem, taking a regular expression String as parameter and returning an array of Strings.

    In your case you wold have to write:

    String[] words = str.split(" ");

    and it will do the same as the StringTokenizer but with only one line of code.

    Happy if this helped you in any way.
    bugvice

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    Good point - I forgot about that - I've only got 1.3 at work
    ArchAngel.
    O:-)

  7. #7
    Join Date
    Oct 2003
    Posts
    12

    Thank you all for your help

    thanks a lot

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links