473,503 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I will try to answer your question soon ...

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

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
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
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
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 *
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 (...)
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 *
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 (...)
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10436
by: cassandra.flowers | last post by:
Hi, I am using VB6 and want to extract text from a string. But ONLY take out words that begin with 't' or 'd'. The mainstring is input by the user into 'txtMain' and then by clicking a command...
8
2179
by: Rick | last post by:
I have a program that reads from a file. In the file are a series of words. I read in all the words into a string array, find the average length, count the number of words, display the longest...
5
3114
by: derrick | last post by:
I used to program text RPG's in C back in high school, but I was very crude at it and only learned what I needed to get the job done. I am now about to graduate college with a degree in English and...
22
13724
by: ineedyourluvin1 | last post by:
Hello all! I've been looking for a way to strip characters from strings such as a comma. This would be great for using a comma as a delimiter. I show you what I have right now. ...
17
8081
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found...
7
43561
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring)....
23
9455
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
10
6204
by: mattias.k.nyberg | last post by:
So Im trying to learn to program with C#. And I have this question about why the string array won't work in the first class but it does in the second. To me it looks like they do the exact same...
7
2354
by: Matt | last post by:
I am attempting to reformat a string, inserting newlines before certain phrases. For example, in formatting SQL, I want to start a new line at each JOIN condition. Noting that strings are...
9
2081
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.