473,725 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get first N words of a string

Hello,

I have a string which holds a text.
Is it possible to create a substring which uses the first N words of
that string?

Thanks,

Miguel

Sep 30 '06 #1
8 4993
Not exactly. The substring method will only return N characters. What you
could do instead is find the Nth space. Once you find the Nth space you know
where the Nth word is. Another way would simply to break the string itself
into a string array by breaking the string on a space, which would be the
normal break between words. For example string[] words = myString.Split( '
',N);

N is the maximum number of substrings to return, hence the maximum
number of words. You could then iterate the array to concatenate them back
into a string.
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"shapper" <md*****@gmail. comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hello,

I have a string which holds a text.
Is it possible to create a substring which uses the first N words of
that string?

Thanks,

Miguel

Sep 30 '06 #2
And one more question related with this:

How to create a substring of a string with all the words until the
first "." is found?

Thanks,
Miguel
Mark Fitzpatrick wrote:
Not exactly. The substring method will only return N characters. What you
could do instead is find the Nth space. Once you find the Nth space you know
where the Nth word is. Another way would simply to break the string itself
into a string array by breaking the string on a space, which would be the
normal break between words. For example string[] words = myString.Split( '
',N);

N is the maximum number of substrings to return, hence the maximum
number of words. You could then iterate the array to concatenate them back
into a string.
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"shapper" <md*****@gmail. comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hello,

I have a string which holds a text.
Is it possible to create a substring which uses the first N words of
that string?

Thanks,

Miguel
Sep 30 '06 #3
This is one way, assuming a single-space is the word separator:

string nWordStr = String.Join (" ", originalString. Split (' '), 0, N);

where N is the number of words to include in the substring from the original
string.

Other option is to find the index of the Nth space in the org string and do
a substring using that position.

"shapper" <md*****@gmail. comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hello,

I have a string which holds a text.
Is it possible to create a substring which uses the first N words of
that string?

Thanks,

Miguel

Sep 30 '06 #4
That one works great!

Could you tell me how would I check if the string has more then 20
words?

Thanks,
Miguel

Siva M wrote:
This is one way, assuming a single-space is the word separator:

string nWordStr = String.Join (" ", originalString. Split (' '), 0, N);

where N is the number of words to include in the substring from the original
string.

Other option is to find the index of the Nth space in the org string and do
a substring using that position.

"shapper" <md*****@gmail. comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hello,

I have a string which holds a text.
Is it possible to create a substring which uses the first N words of
that string?

Thanks,

Miguel
Sep 30 '06 #5
Here you go:

int numOfWords = 0;
int curWB = -1;

do
{
curWB = orginialString. IndexOf(' ', curWB + 1);
if (++numOfWords 20) break;
}
while (curWB >= 0);

if (numOfWords 20)
{
Console.WriteLi ne("more than 20");
}
else
{
Console.WriteLi ne("less than or equal to 20");
}
"shapper" <md*****@gmail. comwrote in message
news:11******** *************@e 3g2000cwe.googl egroups.com...
That one works great!

Could you tell me how would I check if the string has more then 20
words?

Thanks,
Miguel

Siva M wrote:
This is one way, assuming a single-space is the word separator:

string nWordStr = String.Join (" ", originalString. Split (' '), 0, N);

where N is the number of words to include in the substring from the
original
string.

Other option is to find the index of the Nth space in the org string and
do
a substring using that position.

"shapper" <md*****@gmail. comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hello,

I have a string which holds a text.
Is it possible to create a substring which uses the first N words of
that string?

Thanks,

Miguel
Sep 30 '06 #6
string mySubstring = myString.Split( 0,myString.Inde xOf('.'));

Basically it will find the period as a character denoted by '.' and pass the
index of that to the limiter of the split method. If the result is off, you
can just subtract 1 from the indexof such as

myString.Split( 0,myString.Inde xOf('.') - 1). Sometimes when working with the
index you have to tweak a number or two to get it just right.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"shapper" <md*****@gmail. comwrote in message
news:11******** *************@i 3g2000cwc.googl egroups.com...
And one more question related with this:

How to create a substring of a string with all the words until the
first "." is found?

Thanks,
Miguel
Mark Fitzpatrick wrote:
>Not exactly. The substring method will only return N characters. What you
could do instead is find the Nth space. Once you find the Nth space you
know
where the Nth word is. Another way would simply to break the string
itself
into a string array by breaking the string on a space, which would be the
normal break between words. For example string[] words = myString.Split( '
',N);

N is the maximum number of substrings to return, hence the maximum
number of words. You could then iterate the array to concatenate them
back
into a string.
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"shapper" <md*****@gmail. comwrote in message
news:11******* *************** @i3g2000cwc.goo glegroups.com.. .
Hello,

I have a string which holds a text.
Is it possible to create a substring which uses the first N words of
that string?

Thanks,

Miguel

Sep 30 '06 #7
Hi Mark,

I did try that but I am allways getting an error even when I add "-1",
"-2", etc.

The error is:
Attempted to operate on an array with the incorrect number of
dimensions.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.RankExce ption: Attempted to operate on an
array with the incorrect number of dimensions.

How can I figure out what is going on.
I know my variable is a string. It worked fine with the first 20 words
code.

Thanks,
Miguel
Mark Fitzpatrick wrote:
string mySubstring = myString.Split( 0,myString.Inde xOf('.'));

Basically it will find the period as a character denoted by '.' and pass the
index of that to the limiter of the split method. If the result is off, you
can just subtract 1 from the indexof such as

myString.Split( 0,myString.Inde xOf('.') - 1). Sometimes when working with the
index you have to tweak a number or two to get it just right.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"shapper" <md*****@gmail. comwrote in message
news:11******** *************@i 3g2000cwc.googl egroups.com...
And one more question related with this:

How to create a substring of a string with all the words until the
first "." is found?

Thanks,
Miguel
Mark Fitzpatrick wrote:
Not exactly. The substring method will only return N characters. What you
could do instead is find the Nth space. Once you find the Nth space you
know
where the Nth word is. Another way would simply to break the string
itself
into a string array by breaking the string on a space, which would be the
normal break between words. For example string[] words = myString.Split( '
',N);

N is the maximum number of substrings to return, hence the maximum
number of words. You could then iterate the array to concatenate them
back
into a string.
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"shapper" <md*****@gmail. comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hello,

I have a string which holds a text.
Is it possible to create a substring which uses the first N words of
that string?

Thanks,

Miguel
Sep 30 '06 #8
Hi,

I tried what you didn't but I wasn't able to make it work.
I used the following:
Dim MySubstring As String = myString.Substr ing(0,
myString.IndexO f("."c) + 1)

I placed the "+1" to include the "." in the substring.

Thanks,
Miguel
Mark Fitzpatrick wrote:
string mySubstring = myString.Split( 0,myString.Inde xOf('.'));

Basically it will find the period as a character denoted by '.' and pass the
index of that to the limiter of the split method. If the result is off, you
can just subtract 1 from the indexof such as

myString.Split( 0,myString.Inde xOf('.') - 1). Sometimes when working with the
index you have to tweak a number or two to get it just right.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"shapper" <md*****@gmail. comwrote in message
news:11******** *************@i 3g2000cwc.googl egroups.com...
And one more question related with this:

How to create a substring of a string with all the words until the
first "." is found?

Thanks,
Miguel
Mark Fitzpatrick wrote:
Not exactly. The substring method will only return N characters. What you
could do instead is find the Nth space. Once you find the Nth space you
know
where the Nth word is. Another way would simply to break the string
itself
into a string array by breaking the string on a space, which would be the
normal break between words. For example string[] words = myString.Split( '
',N);

N is the maximum number of substrings to return, hence the maximum
number of words. You could then iterate the array to concatenate them
back
into a string.
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"shapper" <md*****@gmail. comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
Hello,

I have a string which holds a text.
Is it possible to create a substring which uses the first N words of
that string?

Thanks,

Miguel
Sep 30 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
17730
by: DrBob | last post by:
gcc 3.3 MAC OS X. I have a string that has trailing spaces in it that I want removed. So i have a variable: string x("abcd "); x.trim() isn't an implemented method. Is there a method I don't know about? What do I do to extent the string class such that there is a method to
5
3130
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,...
7
7077
by: herrcho | last post by:
i'm in the course of learning C, and found these two words "string, string literal" confusing me.. I'd like to know the difference between them.. Thank you
7
43614
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.
7
25787
by: Anat | last post by:
Hi, What regex do I need to split a string, using javascript's split method, into words-array? Splitting accroding to whitespaces only is not enough, I need to split according to whitespace, comma, hyphen, etc... Is there a regex that does the trick? Thanks, Anat.
7
8582
by: Felix85 | last post by:
I am trying to make a command interpreter for a mud that i am working on the problem i am having right now is that i cannot convert the string into a char array. This is the error I am getting now: In file included from src/mud.cpp:3: src/command.h: In static member function `static void command::getCommand(std::string)': src/command.h:38: error: incompatible types in assignment of `const
5
1947
by: isaac86 | last post by:
how to tranlate words(string) to number? for example, char string="abcd" my output will show "abcd is 1234"
6
2497
by: Alexnb | last post by:
Uhm, "string" and "non-string" are just that, words within the string. Here shall I dumb it down for you? string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes text6 no text7 yes text8" It doesn't matter what is in the string, I want to be able to know exactly how many "yes"'s there are. I also want to know what is after each, regardless of length. So, I want to
0
1360
by: Isabella Cana | last post by:
I need to plot this information. I wrote the code to calculate the frequency of words in a file, but I need it to plot the 15 most frequent words in a bar graph. I really don't even know how to start. Any help? import string def compItems((w1,c1), (w2,c2)): if c1 > c2: return - 1 elif c1 == c2: return cmp(w1, w2) else:return 1
9
3605
by: keydrive | last post by:
Hi, I need a regular expression for capturing the first alpha string in a document not necessarily on the first line match first instance Here is a few tried permutations *
0
8872
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
8747
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
9246
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
9162
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
5997
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
4505
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3211
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
2
2619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2150
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.