String. Get words ... |
shapper posted on Thursday, October 09, 2008 7: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 ... |
Duggi posted on Thursday, October 09, 2008 7: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 ... |
Duggi posted on Thursday, October 09, 2008 7:55 AM
|
I apologize, this is not to make fun of you,,, Its really a
challenging logic...
-Cnu |
 |
|
String. Get words ... |
Peter Duniho posted on Thursday, October 09, 2008 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 |
 |
|
String. Get words ... |
shapper posted on Sunday, October 12, 2008 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 ... |
shapper posted on Sunday, October 12, 2008 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. |
 |
|