473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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 4966
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.googlegro ups.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.googlegro ups.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.googlegro ups.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.googlegro ups.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.WriteLine("more than 20");
}
else
{
Console.WriteLine("less than or equal to 20");
}
"shapper" <md*****@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.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.googlegro ups.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.IndexOf('.'));

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.IndexOf('.') - 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*********************@i3g2000cwc.googlegrou ps.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.googlegr oups.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.RankException: 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.IndexOf('.'));

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.IndexOf('.') - 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*********************@i3g2000cwc.googlegrou ps.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.googlegro ups.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.Substring(0,
myString.IndexOf("."c) + 1)

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

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

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.IndexOf('.') - 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*********************@i3g2000cwc.googlegrou ps.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.googlegro ups.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
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...
5
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...
7
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
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)....
7
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,...
7
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...
5
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
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...
0
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...
9
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.