Is there anything in C# that does the same function as strtok in C++. For example if I had the following C++ program how would it be written in C#?

// crt_strtok.c
/* In this program, a loop uses strtok
 * to print all the tokens (separated by commas
 * or blanks) in the string named "string".
 */

#include <string.h>
#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;

int main( void )
{
   printf( "Tokens:\n" );
   /* Establish string and get the first token: */
   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s\n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
}

Thanx,
~Atrus

Never mind I found what I was looking for.

string words = "this is a list of words, with: a bit of punctuation."; 
            string [] split = words.Split(new Char [] {' ', ',', '.', ':'}); 
            foreach (string s in split) 
            { 
                if (s.Trim() != "") Console.WriteLine(s); 
            }

Thanx,
~Atrus

Ok I know I said I had solved this issue, but I've found a new issue. when using the following code (please note this was taken directly from http://www.java2s.com/Tutorial/CSharp/0100__String/Splitstringsbythreetokens.htm)

Using System; 
 
class MainClass { 
  public static void Main() { 
    string str = "while if for, public class do."; 
    char[] seps = {' ', '.', ',' }; 
 
    // Split the string into parts. 
    string[] parts = str.Split(seps); 
    Console.WriteLine("Pieces from split: "); 
    for(int i=0; i < parts.Length; i++) 
      Console.WriteLine(parts[i]); 
    
  } 
}

You get the following output:

Pieces from split:
while
if
for

public
class
do

This means that the length of part is 7 when it should be 6. How would one fix this? Am I missing something?

It appears that it does it correctly because the space after the comma is also a token.
You can either add a new sep ", " (with a space) or break the string into multipe Splits, and combine them later.

It appears that it does it correctly because the space after the comma is also a token.
You can either add a new sep ", " (with a space) or break the string into multipe Splits, and combine them later.

The only thing is that won't work (I've tried it before)
if you use double quotes ("") you get this error

Error 2 Argument '1': cannot convert from 'string' to 'char[]'

and if you use single quotes ('') you get this error

Error 1 Too many characters in character literal

Any other suggestions? (short of multiple split/joins).

The code below returns the 6 parts you were looking for.
The seps var is now a string array instead of char array.
Using the string array in Spit with the remove emptry entries option.

string str = "while if for, public class do.";
            string[] seps = { " ", ".", "," };
            string[] parts = str.Split(seps,StringSplitOptions.RemoveEmptyEntries);
            string c = "";
            foreach(string z in parts)
                c += z+Environment.NewLine;
            MessageBox.Show(c,string.Format("{0} elements",parts.Length));

If this works for you (as it does for me), pleaase mark this thread as solved.
Thanks,
Jerry

Ok that worked. I thought that .Split only accepted char and not strings, so why does it allow a string array like that? (More of a curious question than anything)

Thanks for your help!!

Ok that worked. I thought that .Split only accepted char and not strings, so why does it allow a string array like that? (More of a curious question than anything)

The reason is that tokens come in all shapes and sizes. For example, some Help files use 4 inline characters to desingnate the end of a topic. In order to handle something like
"&*%^" as a token they provided the string array as well as a string of values to satisfy that requirement.

// Jerry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.