LastIndexOfAny
(1)
StringBuilder
(1)
Text.LastIndexOfAny
(1)
String.Split
(1)
Substring
(1)
Split
(1)
Join
(1)
Body.Split
(1)

String. Get words ...

Asked By shapper
09-Oct-08 07:55 AM
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

String. Get words ...

Asked By Duggi
09-Oct-08 07:55 AM
Now I remember my school days.. My teacher was teaching me
permitations
nPn =3D some formula...

I will try to answer your question soon ...

-Cnu

String. Get words ...

Asked By Duggi
09-Oct-08 07:55 AM
I apologize, this is not to make fun of you,,, Its really a
challenging logic...

-Cnu

String. Get words ...

Asked By Peter Duniho
08-Oct-08 02:44 PM
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
append them to your output.

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
String. Get words ...
Asked By shapper
12-Oct-08 12:00 AM
On Oct 8, 7:44=A0pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
can =A0
=A0
=A0
=A0
e =A0
=A0
ou =A0
=A0
=A0
=A0
=A0

I am using the following:

string excerpt =3D 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 (...)
String. Get words ...
Asked By shapper
12-Oct-08 12:00 AM
m
scan =A0
ou =A0
se =A0
=A0
=A0
ire =A0
=A0
you =A0
ds =A0
=A0
o =A0
=A0
rk =A0

Or maybe:

private static string Excerpt(string text, int length)
{
char[] chars =3D {'  ', ',', '.', '?', '!', ':', ';'};
int index =3D 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 =3D 0 to i =3D index. I suppose this
also removes the last punctuation mark if it exists.
String. Get words ...
Asked By Peter Duniho
09-Oct-08 11:04 PM
Sure, that works fine as long as you don't care about character count.
But you do:


So, you need to do what String.Join() does, just write it out yourself and
track the character count too.


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
Post Question To EggHeadCafe