对于一个字符串,怎么实现获取其右边n位或者左边n位的字符,需要用哪个类的哪个函数,要include哪个头文件啊,愿大侠们快快相救

解决方案 »

  1.   

    对于一个字符串,怎么实现获取其右边n位或者左边n位的字符,需要用哪个类的哪个函数,要include哪个头文件啊,愿大侠们快快相救
    ==用CString类的Left函数和Right函数可以完成你的工作。
      

  2.   

    CString的Left,Right可以方便 的截取
      

  3.   

    CString str;
    str.Left(n);//取左边n个
    str.Right(n);
    str.GetAt(n);
    ......
      

  4.   

    #include <string.h>char a[] = 'This is a string!';
    char b[255];char *right(char *a, int n)
    {
      if (n <= strlen(a))  
        return &(a[0]) + len(a) - n;
      else
        return NULL;
    }char * left(char *a, int n)
    {
      if n <= strlen(a)  {
        for(int i = 0; i < n; i++)
          b[i] = a[i];
        b[i] = '\x0';
        return b;
      } else
      return NULL;
    }
      

  5.   

    最好,使用CString,如果使用string.h,好像有安全问题。
      

  6.   

    CString里的方法和操作强的很,好好看看吧
    AllocSysString Allocates a BSTR from CStringT data. 
    AnsiToOem      Makes an in-place conversion from the ANSI character set to the OEM character set. 
    AppendFormat  Appends formatted data to an existing CStringT object. 
    Collate       Compares two strings (case sensitive, uses locale-specific information). 
    CollateNoCase Compares two strings (case insensitive, uses locale-specific information). 
    Compare       Compares two strings (case sensitive). 
    CompareNoCase   Compares two strings (case insensitive). 
    Delete        Deletes a character or characters from a string. 
    Find          Finds a character or substring inside a larger string. 
    FindOneOf     Finds the first matching character from a set. 
    Format        Formats the string as sprintf does. 
    FormatMessage Formats a message string. 
    FormatMessageV Formats a message string using a variable argument list. 
    FormatV Formats the string using a variable list of arguments. 
    GetEnvironmentVariable Sets the string to the value of the specified environment variable. 
    Insert Inserts a single character or a substring at the given index within the string. 
    Left Extracts the left part of a string. 
    LoadString Loads an existing CStringT object from a Windows resource. 
    MakeLower Converts all the characters in this string to lowercase characters. 
    MakeReverse Reverses the string. 
    MakeUpper Converts all the characters in this string to uppercase characters. 
    Mid Extracts the middle part of a string. 
    OemToAnsi Makes an in-place conversion from the OEM character set to the ANSI character set. 
    Remove Removes indicated characters from a string. 
    Replace Replaces indicated characters with other characters. 
    ReverseFind Finds a character inside a larger string; starts from the end. 
    Right Extracts the right part of a string. 
    SetSysString Sets an existing BSTR object with data from a CStringT object. 
    SpanExcluding Extracts characters from the string, starting with the first character, that are not in the set of characters identified by pszCharSet. 
    SpanIncluding Extracts a substring that contains only the characters in a set. 
    Tokenize Extracts specified tokens in a target string. 
    Trim Trims all leading and trailing whitespace characters from the string. 
    TrimLeft Trims leading whitespace characters from the string. 
    TrimRight Trims trailing whitespace characters from the string. 
      

  7.   

    强烈支持CString
    不过char也有自己的好处啦
      

  8.   

    example for CString::Left
    CString s( _T("abcdef") );
    ASSERT( s.Left(2) == _T("ab") );example for CString::Right
    CString s( _T("abcdef") );
    ASSERT( s.Right(2) == _T("ef") );
      

  9.   

    强烈支持用CString,可以看一下参考手册。
    CString Class Members
    Construction
    The String as an Array
    Assignment/Concatenation
    Comparison
    Extraction
    Other Conversions
    Searching
    Archive/Dump
    Buffer Access
    Windows-SpecificConstruction CString Constructs CString objects in various ways. 
    The String as an Array GetLength Returns the number of characters in a CString object. For multibyte characters, counts each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters. 
    IsEmpty Tests whether a CString object contains no characters. 
    Empty Forces a string to have 0 length. 
    GetAt Returns the character at a given position. 
    operator [] Returns the character at a given position — operator substitution for GetAt. 
    SetAt Sets a character at a given position. 
    operator LPCTSTR  Directly accesses characters stored in a CString object as a C-style string. 
    Assignment/Concatenation operator = Assigns a new value to a CString object. 
    operator + Concatenates two strings and returns a new string. 
    operator += Concatenates a new string to the end of an existing string. 
    Comparison operator == <, etc. Comparison operators (case sensitive). 
    Compare Compares two strings (case sensitive). 
    CompareNoCase Compares two strings (case insensitive). 
    Collate Compares two strings (case sensitive, uses locale-specific information). 
    CollateNoCase Compares two strings (case insensitive, uses locale-specific information). 
    Extraction Mid Extracts the middle part of a string (like the Basic MID$ function). 
    Left Extracts the left part of a string (like the Basic LEFT$ function). 
    Right Extracts the right part of a string (like the Basic RIGHT$ function). 
    SpanIncluding Extracts a substring that contains only the characters in a set. 
    SpanExcluding Extracts a substring that contains only the characters not in a set. 
    Other Conversions MakeUpper Converts all the characters in this string to uppercase characters. 
    MakeLower Converts all the characters in this string to lowercase characters. 
    MakeReverse Reverses the characters in this string. 
    Replace Replaces indicated characters with other characters. 
    Remove Removes indicated characters from a string. 
    Insert Inserts a single character or a substring at the given index within the string. 
    Delete Deletes a character or characters from a string. 
    Format Format the string as sprintf does. 
    FormatV Formats the string as vsprintf does. 
    TrimLeft Trim leading whitespace characters from the string. 
    TrimRight Trim trailing whitespace characters from the string. 
    FormatMessage Formats a message string. 
    Searching Find Finds a character or substring inside a larger string. 
    ReverseFind Finds a character inside a larger string; starts from the end. 
    FindOneOf Finds the first matching character from a set. 
    Archive/Dump operator << Inserts a CString object to an archive or dump context. 
    operator >> Extracts a CString object from an archive. 
    Buffer Access GetBuffer Returns a pointer to the characters in the CString. 
    GetBufferSetLength Returns a pointer to the characters in the CString, truncating to the specified length. 
    ReleaseBuffer Releases control of the buffer returned by GetBuffer. 
    FreeExtra Removes any overhead of this string object by freeing any extra memory previously allocated to the string. 
    LockBuffer Disables reference counting and protects the string in the buffer. 
    UnlockBuffer Enables reference counting and releases the string in the buffer. 
    Windows-Specific AllocSysString Allocates a BSTR from CString data. 
    SetSysString Sets an existing BSTR object with data from a CString object. 
    LoadString Loads an existing CString object from a Windows resource. 
    AnsiToOem Makes an in-place conversion from the ANSI character set to the OEM character set. 
    OemToAnsi Makes an in-place conversion from the OEM character set to the ANSI character set.