.NET Framework - 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
LastIndexOfAny
(1)
StringBuilder
(1)
Text.LastIndexOfAny
(1)
String.Split
(1)
Substring
(1)
Split
(1)
Join
(1)
Body.Split
(1)
Duggi replied...
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
Duggi replied...
I apologize, this is not to make fun of you,,, Its really a
challenging logic...
-Cnu
Peter Duniho replied...

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
shapper replied...
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 (...)
shapper replied...
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.
Peter Duniho replied...
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
Strange StringBuilder behavior .NET Framework PROGRAM StringBuilder sbTest1 = new StringBuilder(); StringBuilder sbTest2 = new StringBuilder(); for (int i = 1; i < = 54; i++) sbTest1.Append("a"); System.Diagnostics.Debug.WriteLine("sbTest1 54, Length: 54 QUESTION as seen from the output if the string is appeneded to StringBuilder (sbTest1) from loop the capacity increases by multiple of two and Lenght would show as why this behaviour? is this intentional for some good reason or a bug C# Discussions StringBuilder (1) The capacity starts (by default) as 16. When adding data (that exceeds the capacity implementation details" - fun* to know, but don't rely on it - just know that the StringBuilder will make suitable attempts to work efficiently with the strings. Marc * = for small values of
StringBuilder .NET Framework Hello, I am creating the body of a mail message to be sent using StringBuilder: StringBuilder body = new StringBuilder(); body.AppendFormat("{0}, ", userName); body.AppendLine(); body.AppendLine(); body.AppendLine("How are you?"); How should create empty lines? User various ApendLine as n the previous example or maybe using <br / > : StringBuilder body = new StringBuilder(); body.AppendFormat("{0}, ", userName); body.AppendLine("How are you?"); or maybe some other way? Thanks, Miguel C# Discussions StringBuilder.AppendLine (1) Environment.NewLine (1) StringBuilder (1) AppendFormat (1) Environment (1) AppendLine (1) Body.AppendFormat (1) Body.AppendLine (1) I don
StringBuilder much much faster and better than String for concatenation !!! .NET Framework StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical difference? The hardware running this program was a Pentium IV with 2 GB RAM. RL / / stringbuilder much faster than string in concatenation / / / / / / / / / / / / / / using System; using System.Collections.Generic; using System.Linq are: {0}, {1}", myUpdateTime.txtConcatTime, myUpdateTime.txtStringBTime); } } } / * * OUTPUT * results: * for 1000 iterations: string = 10.01ms; stringbuilder = 0 * for 5000 iterations: string = 410.6ms; stringbuilder = 0 * for 50k iterations: sring = 79013 ms; stringbuilder = 0; * for 10k iterations : string = 1772.5 ms; stringbuilder = 0; * for 75k iterations : string = 186237
performance: StringBuilder vs StringBuilder & XSLT .NET Framework hello, im having a discussion w / one of my associates, and we XML-FO, or XML). there are two options: 1) iterate thru the collections, using a StringBuilder to build the final verbose doc in code. lots of presentation tags, which is sorta a bummer cuz its all static strings if using StringBuilder. 2) iterate thru the collections, using a StringBuilder to build a smaller data-only XML, then use an XSLT doc to perform transform speed differences of the two, if any. what are you thoughts? thanks, sm Xml Discussions StringBuilder (1) KeyID (1) PDFs (1) XmlDocument (1) XmlWriter (1) Mannheim (1) Höhrmann (1) Björn (1 XmlWriter to construct your XML. - - Martin Honnen - -- MVP XML http: / / JavaScript.FAQTs.com / keywords: performance:, StringBuilder, vs, StringBuilder, &, XSLT description: hello, im having a discussion w / one of my associates, and we're