CString user="";
CString mss="";
CString msg="user:hello~";
int flag=1;
for(int i=0,j=0;(msg.GetAt(i))!=_T("~");i++)
{
if(flag==1&&(msg.GetAt(i))!=_T(":"))

{
user.SetAt(i,msg[i]);

}
else
{
flag=0;
mss.SetAt(j++,msg[++i]);
}
}
我想得到的效果是
user="user"
mss="hello"
请各位大侠帮帮小弟,谢谢阿

解决方案 »

  1.   

    CString user="";
    CString mss="";
    CString msg="user:hello~";
    int i = msg.Find(":",0);
    user =msg.Left(i) ; 
    msg.Delete(0,i+1);
    i = msg.Find("~",0);
    mss = msg.Left(i) ;
      

  2.   

    既然用了CString 了,就没有必要这么麻烦了。
    --------------------------
    user=msg.Left(msg.Find(':'));
    tmp=msg.Right(msg.Find(':'));
    mss=tmp.Left(tmp.Find('~'));
    ---------------------------------
      

  3.   

    CString user="";
    CString mss="";
    CString msg="user:hello~";int a=msg.Find(":");
    int b=msg.Find("~");
    user=msg.Mid(0,a);
    mss=msg.Mid(a+1,b);
      

  4.   

    先对输入字符串做一个正确检查char cUser[24];
    char cHello[24];再用  scanf("user:hello~","%s:%s~",cUser,cHello);试一下吧
      

  5.   

    哦,谢谢各位了另外想问一下,Saimen(爱已随你而去...小茜)这位老兄的scanf("user:hello~","%s:%s~",cUser,cHello);能否解释一下
      

  6.   

    :)  写错了
    sscanf(strOneItem,"共有模拟量:%d",&nCounter);sscanf, swscanf
    Read formatted data from a string.int sscanf( const char *buffer, const char *format [, argument ] ... );int swscanf( const wchar_t *buffer, const wchar_t *format [, argument ] ... );Routine Required Header Compatibility 
    sscanf <stdio.h> ANSI, Win 95, Win NT 
    swscanf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT 
    For additional compatibility information, see Compatibility in the Introduction.
    Return ValueEach of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.ParametersbufferStored dataformatFormat-control stringargumentOptional argumentsFor more information, see Format Specifications.ResThe sscanf function reads data from buffer into the location given by each argument. Every argument must be a pointer to a variable with a type that corresponds to a type specifier in format. The format argument controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function; see scanf for a complete description of format. If copying takes place between strings that overlap, the behavior is undefined.swscanf is a wide-character version of sscanf; the arguments to swscanf are wide-character strings. sscanf does not handle multibyte hexadecimal characters. swscanf does not handle Unicode fullwidth hexadecimal or “compatibility zone” characters. Otherwise, swscanf and sscanf behave identically.Example/* SSCANF.C: This program uses sscanf to read data items
     * from a string named tokenstring, then displays them.
     */#include <stdio.h>void main( void )
    {
       char  tokenstring[] = "15 12 14...";
       char  s[81];
       char  c;
       int   i;
       float fp;   /* Input various data from tokenstring: */
       sscanf( tokenstring, "%s", s );
       sscanf( tokenstring, "%c", &c );
       sscanf( tokenstring, "%d", &i );
       sscanf( tokenstring, "%f", &fp );   /* Output the data read */
       printf( "String    = %s\n", s );
       printf( "Character = %c\n", c );
       printf( "Integer:  = %d\n", i );
       printf( "Real:     = %f\n", fp );
    }
    OutputString    = 15
    Character = 1
    Integer:  = 15
    Real:     = 15.000000
      

  7.   

    sscanf  就是从一个给定的字符串中按你所给出的格式读出你指定的数据sscanf("user:hello~","%s:%s~",cUser,cHello);从 "user:hello~" 这个给定的字符串中
    按 "字符串:字符串~"这个格式
    读出字符串 cUser 与 cHello读出后应是:  cUser == "user"   cHello == "hello"
      

  8.   

    牛!!!!!!!!!!!!!!!!!!!!!!!!!!!Saimen(爱已随你而去...小茜) 
    谢谢你了老哥,能不能交个朋友 阿我的QQ:33529278