473,651 Members | 2,716 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 2923
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.comwr ote:
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...@nn owslpianmk.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...@nn owslpianmk.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.LastIndexO fAny(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
10504
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 button, all the words that begin with 't' or 'd' will be extracted and appear in a second text box, txtExtract. So, i know that I have to search through the main string for the character "d", then extract the following characters until it...
8
2190
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 word and how long it is, and display the smallest word and how long it is. All that is working fine, but what I need to do it format certain words that contain a period at the end of them (ex: a word at the end of a sentence), words with quotes...
5
3123
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 have had an interest in the structure of language. In order to begin with my little program, I need to be able to take a string like "The dog jumped." and be able to take the first word from the sentence and assign it to a string, such as word1,...
22
13758
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. #include<iostream> #include<string> int main(int argc, char *argv) {
17
8117
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 characters") Else Debug.WriteLine("Did NOT find characters!") End If
7
43592
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). Is there something equivalent in C#, preferably built-in (assumed better performance), or sample code? Thanks in advance.
23
9493
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 will print literally as {{-Hello}} World, instead of
10
6252
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 thing. class Test { string words; public Test() { words = { "", "", "" }; // This doesn't work }
7
2362
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 immutable, I thought it best to spllit the string at the key points, then join with '\n'. Regexps can seem the best way to identify the points in the string ('LEFT.*JOIN' to cover 'LEFT OUTER JOIN' and 'LEFT JOIN'), since I need to identify multiple...
9
2093
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, this is not a homework assignment. In fact it was a question on my class midterm that everyone bombed. Unfortunately we never covered this in class prior to the midterm. Anyway, please help if you would be so kind. Here is what I have. I...
0
8352
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8697
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.