Results 1 to 3 of 3
  1. #1
    robWhittle is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default split() function problem.

    Hi guys. I'm trying to develop a small program for my final year project at university, but am having some issues with the split function.

    What I'm attempting to do is split a sentence into its composite words, each one being held in an array, which is then compared in turn against a list of words in another array.

    My problem comes when I attempt to compare words that come at the end of a sentence or before a comma. I want to split the words whilst also removing any such punctuation. Can you suggest a way of doing this? Currently my splitting function looks like this:

    for (int i = 0; i < sentences.length; i++)
    {

    //Split the string into its seperate elements
    holdingString = sentences[i];
    words = holdingString.split(" ");
    }

    Thanks for the help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm no regular expressions expert, but I'd look to split with a regex that combines white space and punctuation, perhaps something as simple as "[\\s\\.,]+".

    e.g.,
    Java Code:
    import java.util.Arrays;
    
    public class SplitTest {
      public static void main(String[] args) {
        String test = "Hello world.  It's common knowledge that fubars rule, and that snafus drool.";
        String regex = "[\\s\\.,]+";
        
        String[] tokens = test.split(regex);
        System.out.println(Arrays.toString(tokens));
        
      }
    }

  3. #3
    robWhittle is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. split problem
    By mac in forum New To Java
    Replies: 4
    Last Post: 01-02-2010, 04:46 AM
  2. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  3. Problem with split function
    By a.tajj in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 03:30 AM
  4. problem with split method
    By abhiN in forum New To Java
    Replies: 7
    Last Post: 02-10-2009, 01:54 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •