Split a string


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Split a string

  1. #1
    Join Date
    Jan 2007
    Posts
    60

    Split a string

    I am trying to split a string so i can count the words but it is not splitting carriage returns...only spaces.

    I have a text file with about 10 lines of text on it with line spaces.

    When the string is printed the last word of the line and the beginning word of the next line are joined together.

    How can I get it so the words are seperated?

    Code:
    public void wordCount()
    	{
    		String[] result = textWord.split("\\s");
    
    		System.out.println("Number of words: "+result.length);
    	}
    The file has 61 words but the result.length is reporting 53 words

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Have you tried to do this with the Scanner class rather than the String class's split method? The default delimiter for Scanner is whitespace (which, it sounds like, you are looking for). It will also take the filename as argument for its constructor, so will do away with your BufferedReader procedure you wrote about, before.

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  2. Replies: 1
    Last Post: 06-05-2001, 06:12 AM
  3. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM
  4. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 07:36 AM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 03:21 AM

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center