Results 1 to 3 of 3
Thread: split() function problem.
- 04-09-2010, 12:51 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
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.
-
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)); } }
- 04-09-2010, 01:34 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
split problem
By mac in forum New To JavaReplies: 4Last Post: 01-02-2010, 04:46 AM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
Problem with split function
By a.tajj in forum New To JavaReplies: 4Last Post: 04-14-2009, 03:30 AM -
problem with split method
By abhiN in forum New To JavaReplies: 7Last Post: 02-10-2009, 01:54 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks