469,631 Members | 1,776 Online

# String. Get words ...

Hello,

I have a string and I need to get as many words possible counting from
the beginning but without exceeding 120 characters. I can't break
words and I the string shouldn't end with a comma, a dot, ...

How can I do this?

Thank You,
Miguel
Oct 8 '08 #1
6 2788
On Oct 8, 10:53*am, shapper <mdmo...@gmail.comwrote:
Hello,

I have a string and I need to get as many words possible counting from
the beginning but without exceeding 120 *characters. I can't break
words and I the string shouldn't end with a comma, a dot, ...

How can I do this?

Thank You,
Miguel
Now I remember my school days.. My teacher was teaching me
permitations
nPn = some formula...

-Cnu
Oct 8 '08 #2
On Oct 8, 11:07*am, Duggi <DuggiSrinivasa...@gmail.comwrote:
On Oct 8, 10:53*am, shapper <mdmo...@gmail.comwrote:
Hello,
I have a string and I need to get as many words possible counting from
the beginning but without exceeding 120 *characters. I can't break
words and I the string shouldn't end with a comma, a dot, ...
How can I do this?
Thank You,
Miguel

Now I remember my school days.. My teacher was teaching me
permitations
nPn = some formula...

-Cnu
I apologize, this is not to make fun of you,,, Its really a
challenging logic...

-Cnu
Oct 8 '08 #3
On Wed, 08 Oct 2008 10:53:15 -0700, shapper <md*****@gmail.comwrote:
I have a string and I need to get as many words possible counting from
the beginning but without exceeding 120 characters. I can't break
words and I the string shouldn't end with a comma, a dot, ...

How can I do this?
First, you need to decide what you mean by "word". Then, you need to scan
through the string looking for words. As you find a complete word, you
then need to append the word to a new string, unless doing so would cause
that new string to exceed your limit of 120 characters, in which case
you're done.

The original string can be a String. Scanning can be done explicitly
yourself one character at a time, or if you don't mind scanning the entire
input all at once before actually processing the words, you can use the
String.Split() method. For the purpose of creating the new string as you
go along, I recommend the StringBuilder class.

Note that if you want to preserve the delimiting characters between words
in your output, you'll need to do the scan explicitly rather than using
String.Split(), so that you have access to those characters and can also

We could just write the code for you but, frankly, we're not on your
payroll and it seems like you ought to go ahead and do _some_ of the work
yourself. :)

Pete
Oct 8 '08 #4
On Oct 8, 7:44*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 08 Oct 2008 10:53:15 -0700, shapper <mdmo...@gmail.comwrote:
I have a string and I need to get as many words possible counting from
the beginning but without exceeding 120 *characters. I can't break
words and I the string shouldn't end with a comma, a dot, ...
How can I do this?

First, you need to decide what you mean by "word". *Then, you need to scan *
through the string looking for words. *As you find a complete word, you*
then need to append the word to a new string, unless doing so would cause*
that new string to exceed your limit of 120 characters, in which case *
you're done.

The original string can be a String. *Scanning can be done explicitly *
yourself one character at a time, or if you don't mind scanning the entire *
input all at once before actually processing the words, you can use the *
String.Split() method. *For the purpose of creating the new string as you *
go along, I recommend the StringBuilder class.

Note that if you want to preserve the delimiting characters between words*
in your output, you'll need to do the scan explicitly rather than using *
String.Split(), so that you have access to those characters and can also *

We could just write the code for you but, frankly, we're not on your *
payroll and it seems like you ought to go ahead and do _some_ of the work*
yourself. *:)

Pete
I am using the following:

string excerpt = String.Join (" ", body.Split (' '), 0, N);

To get the first N words ...

But I am not sure how to count the characters so that excerpt is less
than 120 characters length.

I also need to keep all punctuation marks, I think I am doing that,
with the exception of any that appears at the end ...
....this is because I want to add at the end an ellipsis (...)
Oct 9 '08 #5
On Oct 9, 10:09*pm, shapper <mdmo...@gmail.comwrote:
On Oct 8, 7:44*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 08 Oct 2008 10:53:15 -0700, shapper <mdmo...@gmail.comwrote:
I have a string and I need to get as many words possible counting from
the beginning but without exceeding 120 *characters. I can't break
words and I the string shouldn't end with a comma, a dot, ...
How can I do this?
First, you need to decide what you mean by "word". *Then, you need toscan *
through the string looking for words. *As you find a complete word, you *
then need to append the word to a new string, unless doing so would cause *
that new string to exceed your limit of 120 characters, in which case *
you're done.
The original string can be a String. *Scanning can be done explicitly*
yourself one character at a time, or if you don't mind scanning the entire *
input all at once before actually processing the words, you can use the*
String.Split() method. *For the purpose of creating the new string asyou *
go along, I recommend the StringBuilder class.
Note that if you want to preserve the delimiting characters between words *
in your output, you'll need to do the scan explicitly rather than using*
String.Split(), so that you have access to those characters and can also *
We could just write the code for you but, frankly, we're not on your *
payroll and it seems like you ought to go ahead and do _some_ of the work *
yourself. *:)
Pete

I am using the following:

string excerpt = String.Join (" ", body.Split (' '), 0, N);

To get the first N words ...

But I am not sure how to count the characters so that excerpt is less
than 120 characters length.

I also need to keep all punctuation marks, I think I am doing that,
with the exception of any that appears at the end ...
...this is because I want to add at the end an ellipsis (...)
Or maybe:

private static string Excerpt(string text, int length)
{
char[] chars = {' ', ',', '.', '?', '!', ':', ';'};
int index = text.LastIndexOfAny(chars, length);
return text.Substring(0, index);
}

I try to find the last index of a space or of the most comon
punctuation mark in the desired length, for example 120.
Then substring gets the string from i = 0 to i = index. I suppose this
also removes the last punctuation mark if it exists.
Oct 9 '08 #6
On Thu, 09 Oct 2008 14:09:55 -0700, shapper <md*****@gmail.comwrote:
I am using the following:

string excerpt = String.Join (" ", body.Split (' '), 0, N);

To get the first N words ...
Sure, that works fine as long as you don't care about character count.
But you do:
But I am not sure how to count the characters so that excerpt is less
than 120 characters length.
So, you need to do what String.Join() does, just write it out yourself and
track the character count too.
I also need to keep all punctuation marks, I think I am doing that,
with the exception of any that appears at the end ...
...this is because I want to add at the end an ellipsis (...)
You can modify the technique as necessary to do that. Handling a special
case at the end of the string is easy, and since your punctuation marks
aren't part of the Split() delimiter list, they will remain as part of the
individually split strings.

Pete
Oct 10 '08 #7

### This discussion thread is closed

Replies have been disabled for this discussion.