Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Given this text:

 

This is a $5 million cost reduction program. The company has about 2.6 million distinct inventory items.

 

I am having trouble trying to break the string into "words".

 

The problem I'm having is that split doesn't accept multiple delimiters and even if it did, I couldn't split using the "." character because that would split the "2.6" into 2 different words.

 

I'm not too familiar with regex. It allows multiple delimiters but can it differentiate for a "." that ends a word (or sentence) and one that is embedded in a number ("2.6")?

 

Or is there another way to break string data into individual words to be processed?

 

tia,

flynn

  • *Experts*
Posted

Actually, String.Split() does accept multiple delimeters; they must be of the char datatype. So, for example, if you wanted to split by spaces and by periods:

string sentence = " This is a $5 million cost reduction program. The company has about 2.6 million distinct inventory items.";
string[] words = sentence.Split(new char[] {' ','.'});

 

I agree, however, that this is not a good idea because of the 2.6 figure. Instead, I would split only by spaces and then go through each word and remove trailing periods.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

RegEx:

 

\S*[^\.\s]

 

IOW Any number of non-white-space characters followed by a single character that is neither a period nor a white-space. You can throw other punctuation in there as needed. e.g. comma, semi-colon, etc.

"Who is John Galt?"

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...