String. Get words ...

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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
 
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...

I will try to answer your question soon ...

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

I will try to answer your question soon ...

-Cnu

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

-Cnu
 
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

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 (...)
 
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.
 
Back
Top