CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Get Substring

  1. #1
    Join Date
    Jun 2002
    Posts
    936

    Get Substring

    I do have a string given to me as

    CString strString = "ON,IN,1,0 ms";

    I want to separate the string to another string where I have to look for the comma

    I want the other string to be something like

    CString strString2[4]

    where

    strString2[0] = "ON"
    strString2[1] = "IN"
    strString2[2] = "1"
    strString2[3] = "0ms"

    I want to know what function should I use to split that string on the form above.

  2. #2
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567

    RE: Get substring

    Originally quoted by vcstarter
    I want to separate the string to another string where I have to look for the comma

    If you have VC.net which uses the "new" CSimpleStringT you can use Tokenize which works like the old strtok. Otherwise you can do it with CString::Find, CString::Mid and a while loop.

    TDM

  3. #3
    Join Date
    Apr 2004
    Location
    Munich,Germany
    Posts
    12
    One little function for that would be:
    void ChopString(CString& src,const char seperator,CStringArray& ary)
    {
    int index=0;
    CString leftover,temp;
    do
    {
    index=src.Find(seperator);
    leftover=src.Mid(index+1,src.GetLength()-index-1);
    if(index!=-1) temp=src.Left(index);
    else
    temp=src;
    ary.Add(temp);
    src=leftover;
    }while(index!=-1);

    }

  4. #4
    Join Date
    Feb 2003
    Location
    Bilbao
    Posts
    513
    There is a function that does whatever you want to get substrings.

    It is called AfxExtractSubstring. It is not well documented, but if you type it in VC++ editor you will see its parameters.
    Caronte
    Si tiene solución... ¿por qué te preocupas?
    Si no tiene solución... ¿por qué te preocupas?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured