By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

How to extract words out from a string?

P: n/a
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.
Dec 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Sling wrote:
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.


Assuming your words are space separated, you could try:

string[] words = stringWithSentence.Split ( new char[] { ' ' } );

After this, words will be an array of words. The words come from the
string, and the space character is used as separation (the string is
split on the space character). You could specify one or more separator
character for the Split method.

You can get the number of words you got by using words.Length property,
and can get the ith word by using words[i], which is I think equally
simple as what you describe.

-Lenard
Dec 13 '05 #2

P: n/a
Thanks, Lenard. What if my string s contains: "This is a test string
with more than 1 space in between "? Words(s) from Rexx returns 12, and
word(s,5) returns "string". Would I be able to get something similar back
from C#?

"Lenard Gunda" wrote:
Sling wrote:
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.


Assuming your words are space separated, you could try:

string[] words = stringWithSentence.Split ( new char[] { ' ' } );

After this, words will be an array of words. The words come from the
string, and the space character is used as separation (the string is
split on the space character). You could specify one or more separator
character for the Split method.

You can get the number of words you got by using words.Length property,
and can get the ith word by using words[i], which is I think equally
simple as what you describe.

-Lenard

Dec 13 '05 #3

P: n/a
I tried but failed to find something similar in .NET framework, so I ended up
writing the subroutines myself, as follow:
static string Word(string s, int i) {
string w = "";
int c = 0;
s = s.Trim();
if (s.Length > 0 && i > 0) {
for (int x = 0; x <= s.Length-1; x++) {
if (s.Substring(x,1) == " ") {
while (x <= s.Length-1 && s.Substring(x,1) == " ")
x++;
}
else {
c = c + 1;
if (c == i) {
while (x <= s.Length-1 && s.Substring(x,1) != " ") {
w = w + s.Substring(x,1);
x++;
}
return w;
}
else {
while (x <= s.Length-1 && s.Substring(x,1) != " ")
x++;
}
}
x--;
}
}
return w;
} // end of function Word..

static int Words(string strIn) {
int w = 0;
string str1 = strIn.Trim();
if (str1.Length > 0) {
for (int x = 0; x <= str1.Length-1; x++) {
if (str1.Substring(x,1) == " ")
while (x <= str1.Length-1 && str1.Substring(x,1) == " ")
x++;
else {
w++;
while (x <= str1.Length-1 && str1.Substring(x,1) != " ")
x++;
}
x--;
}
}
return w;
} // end of function Words..

Anyone got anything better (performance wise)?

Thanks, Lenard. What if my string s contains: "This is a test string
with more than 1 space in between "? Words(s) from Rexx returns 12, and
word(s,5) returns "string". Would I be able to get something similar back
from C#?

"Lenard Gunda" wrote:
Sling wrote:
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.


Assuming your words are space separated, you could try:

string[] words = stringWithSentence.Split ( new char[] { ' ' } );

After this, words will be an array of words. The words come from the
string, and the space character is used as separation (the string is
split on the space character). You could specify one or more separator
character for the Split method.

You can get the number of words you got by using words.Length property,
and can get the ith word by using words[i], which is I think equally
simple as what you describe.

-Lenard

Dec 13 '05 #4

P: n/a
Hi,

For your test string the code I provided would return more than 12,
because it would find the empty strings as well. The array would then
contain multiple empty elements. This is the situation with .NET 1.1

In .NET 2.0 you could add an options parameter to the Split method call:

string[] words = stringWithSentence.Split ( new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries );

This would remove the empty elements from the returned array. This is
what you need I think. But this behaviour is only available in .NET 2.0.

-Lenard
Sling wrote:
Thanks, Lenard. What if my string s contains: "This is a test string
with more than 1 space in between "? Words(s) from Rexx returns 12, and
word(s,5) returns "string". Would I be able to get something similar back
from C#?

"Lenard Gunda" wrote:

Sling wrote:
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.


Assuming your words are space separated, you could try:

string[] words = stringWithSentence.Split ( new char[] { ' ' } );

After this, words will be an array of words. The words come from the
string, and the space character is used as separation (the string is
split on the space character). You could specify one or more separator
character for the Split method.

You can get the number of words you got by using words.Length property,
and can get the ith word by using words[i], which is I think equally
simple as what you describe.

-Lenard

Dec 13 '05 #5

P: n/a
Sling <cs***********@hotmail.com> wrote:
I tried but failed to find something similar in .NET framework, so I ended up
writing the subroutines myself, as follow:


<snip>

Aside from anything else, you should know about a couple of performance
things:

1) Taking a single character substring creates a new object needlessly.
Just use the indexer to return a character.

2) Use a StringBuilder to build strings, rather than repeated string
concatenation.

Now, I'd say the best way of doing the main split is to use a regular
expression. For instance:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string x = "This is a test string"+
" with more than 1 space in between ";

foreach (string word in Regex.Split (x, @"\s+"))
{
Console.WriteLine ("'{0}'", word);
}
}
}

The results are:
'This'
'is'
'a'
'test'
'string'
'with'
'more'
'than'
'1'
'space'
'in'
'between'
''

Note the final empty string, because there's a space at the end. If you
don't want that, trim the string first (call String.Trim and use the
result as the input to Regex.Split).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 13 '05 #6

P: n/a
tjb
"Sling" <cs***********@hotmail.com> posted:
I tried but failed to find something similar in .NET framework, so I ended up
writing the subroutines myself, as follow:


<snip>

As Jon said, use a regex. If you want to learn all about
regexes---and I would recommend doing so, as they can be *very*
handy---an online tutorial can be found at
<http://www.regular-expressions.info/>.
Dec 13 '05 #7

P: n/a
Thanks, Jon & tjb. Will give this a try.

"Jon Skeet [C# MVP]" wrote:
Sling <cs***********@hotmail.com> wrote:
I tried but failed to find something similar in .NET framework, so I ended up
writing the subroutines myself, as follow:


<snip>

Aside from anything else, you should know about a couple of performance
things:

1) Taking a single character substring creates a new object needlessly.
Just use the indexer to return a character.

2) Use a StringBuilder to build strings, rather than repeated string
concatenation.

Now, I'd say the best way of doing the main split is to use a regular
expression. For instance:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string x = "This is a test string"+
" with more than 1 space in between ";

foreach (string word in Regex.Split (x, @"\s+"))
{
Console.WriteLine ("'{0}'", word);
}
}
}

The results are:
'This'
'is'
'a'
'test'
'string'
'with'
'more'
'than'
'1'
'space'
'in'
'between'
''

Note the final empty string, because there's a space at the end. If you
don't want that, trim the string first (call String.Trim and use the
result as the input to Regex.Split).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.