Get first N words of a string

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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

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
 
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 said:
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 said:
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
 
That one works great!

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

Thanks,
Miguel
 
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");
}


That one works great!

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

Thanks,
Miguel
 
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 said:
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 said:
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 said:
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
 
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 said:
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 said:
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 said:
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


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
 
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 said:
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 said:
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 said:
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


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
 
Back
Top