473,322 Members | 1,409 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,322 software developers and data experts.

How to extract words out from a string?

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
7 43526
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lydia Shawn | last post by:
hi there, i want to extract the numbers from this example input: bla trigger3 trigger4 trigger1 blabla trigger1 5000.00 trigger3 trigger1 trigger2 trigger2 600.00 trigger4 trigger1 50.00...
9
by: Sharon | last post by:
hi, I want to extract a string from a file, if the file is like this: 1 This is the string 2 3 4 how could I extract the string, starting from the 10th position (i.e. "T") and...
3
by: Maya | last post by:
Hello guys, Is there an easy way to extract individual words that form a string and store them in variables like in this example: String "how are you?" My result would be: var1 = "how"
0
by: kamal9 | last post by:
#include <stdio.h> #include <string.h> void string2Lines(char line, char tokens){ char *token_ptr, token; char *i=";" " " "," "." "?" "!"; token_ptr = strtok(line, i ); while(token_ptr){...
17
by: Umesh | last post by:
Can anyone do it? ARMY1987- what say?
0
by: wbw | last post by:
I am trying to extract capitalized words from text in Excel. I have a list of a combination of brands and products and I am trying to extract out the product attribute from the text. Since the text...
1
by: Edwin.Madari | last post by:
from each line separate out url and request parts. split the request into key-value pairs, use urllib to unquote key-value pairs......as show below... import urllib line = "GET...
0
by: JohanA | last post by:
I´m using Apose.words to extract diagrams from word 2003 documents. It´s no problem to iterate the embedded MSGraph.Chart.8 objects but I can´t read the data from them. Anyone who knows how to do...
3
by: Nen013 | last post by:
So I'm trying to create a function that will extract a word from a string. I'm trying to do this without creating an array. So far I have this: string s = "Hello World"; string t; for (size_t...
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...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.