Thread: CString split function ?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    103

    CString split function ?

    anybody knows if theres any function to split a string by a character.. for example i have:
    MessageŽMessageŽMessageŽMessageŽMessageŽMessageŽ.. ..
    and i want to split all by the Ž character and just get "Message" in arrays of CString
    thanks

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    What you are looking for is to separate a string into tokens. This can be accomplished in C using strtok(). Here is the FAQ on strtok . And below is a sample program that seperates user input into tokens based on " "(spaces).
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    
        int j, i=0; // used to iterate through array
    
        char userInput[81], *token[80]; //user input and array to hold max possible tokens, aka 80.
    
        printf("Enter a line of text to be tokenized: ");
        fgets(userInput, sizeof(userInput), stdin);
    
        token[0] = strtok(userInput, " "); //get pointer to first token found and store in 0
                                           //place in array
        while(token[i]!= NULL) {   //ensure a pointer was found
            i++;
            token[i] = strtok(NULL, " "); //continue to tokenize the string
        }
        
        for(j = 0; j <= i-1; j++) {
            printf("%s\n", token[j]); //print out all of the tokens
        }
    
        return 0;
    }
    And here is my output:
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    hmm was thinking if there is another function in mfc for strings like there is in visual basic :P Split(start number,"string","separator",max splitings)
    anyway thanks i think i have to make my own function for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM