zoukankan      html  css  js  c++  java
  • CString 操作函数

    CString结尾有一个NULL;

    1、int GetLength( ) const;   //返回字符串的长度,不包含结尾的空字符;在MBCS中,8位计位一个;在Unicode中,字符个数?

    例:csStr="ABCDEF中文123456";
        printf("%d",csStr.GetLength());       //16

    2、void MakeReverse( );     //颠倒字符串的顺序

    例:csStr="ABCDEF中文123456";
        csStr.MakeReverse();
        cout<<csStr;                  //654321文中FEDCBA

    3、void MakeUpper( ); void MakeLower( );  将小写字母转换为大写字母;  将大写字母转换为小写字母;

    4、int Compare( LPCTSTR lpsz ) const;   \ 区分大小写比较两个字符串,相等时返回0,大于时返回1,小于时返回-1

    int CompareNoCase( LPCTSTR lpsz ) const;  \不区分大小写比较两个字符串,相等时返回0,大于时返回1,小于时返回-1
    5、int Delete( int nIndex, int nCount = 1 )      \ 删除字符,删除从下标nIndex开始的nCount个字符
    //当nIndex过大,超出对像所在内存区域时,函数没有任何操作。
    //当nIndex为负数时,从第一个字符开始删除。
    //当nCount过大,导致删除字符超出对像所在内存区域时,会发生无法预料的结果。
    //当nCount为负数时,函数没有任何操作。

    6、int Insert( int nIndex, TCHAR ch )
    int Insert( int nIndex, LPCTSTR pstr )   \ 在下标为nIndex的位置,插入字符或字符串。返回插入后对象的长度

    7、int Remove( TCHAR ch );    \移除对象内的指定字符。返回移除的数目

    8、int Replace( TCHAR chOld, TCHAR chNew );

    int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );   \ 替换字串

    9、void TrimLeft( );                                         void TrimRight( );
    void TrimLeft( TCHAR chTarget );                  void TrimRight( TCHAR chTarget );
    void TrimLeft( LPCTSTR lpszTargets );          void TrimRight( LPCTSTR lpszTargets );

    从左删除字符,被删的字符与chTarget或lpszTargets匹配,一直删到第一个不匹配的字符为止
    例:csStr="aaabaacdef";
        csStr.TrimLeft('a');
        cout<<csStr;                //baacdef
        csStr="aaabaacdef";
        csStr.TrimLeft("ab");
        cout<<csStr;                //cdef
    //无参数时删除空格

    10、void Empty( );  \ 清空

    11、BOOL IsEmpty( ) const;  \ 测试对象是否为空,为空时返回零,不为空时返回非零

    12、int Find( TCHAR ch ) const;

    int Find( LPCTSTR lpszSub ) const;

    int Find( TCHAR ch, int nStart ) const;
    int Find( LPCTSTR pstr, int nStart ) const;   \ 查找字串,nStart为开始查找的位置。未找到匹配时返回-1,否则返回字串的开始位置

    13、void Format( LPCTSTR lpszFormat, ... );

    void Format( UINT nFormatID, ... );  格式化对象,与C语言的sprintf函数用法相同

    14、int ReverseFind( TCHAR ch ) const;  从后向前查找第一个匹配,找到时返回下标。没找到时返回-1

    15、void Format( LPCTSTR lpszFormat, ... );

    void Format( UINT nFormatID, ... );   格式化对象,与C语言的sprintf函数用法相同

    16、TCHAR GetAt( int nIndex ) const;    返回下标为nIndex的字符,与字符串的[]用法相同

    void SetAt( int nIndex, TCHAR ch );    给下标为nIndex的字符重新赋值

    17、CString Left( int nCount ) const;    从左取字串

    CString Right( int nCount ) const;     从右取字串

    18、CString Mid( int nFirst ) const;

    CString Mid( int nFirst, int nCount ) const;  从中间开始取字串

    19、LPTSTR GetBuffer( int nMinBufLength );   申请新的空间,并返回指针;不包括末尾; 

    例:csStr="abcde";
        LPTSTR pStr=csStr.GetBuffer(10);
        strcpy(pStr,"12345");
        csStr.ReleaseBuffer();
        pStr=NULL;
        cout<<csStr                 //12345
    //使用完GetBuffer后,必须使用ReleaseBuffer以更新对象内部数据,否则会发生无法预料的结果。

    20、void ReleaseBuffer( int nNewLength = -1 );   使用GetBuffer后,必须使用ReleaseBuffer以更新对象内部数据

    例:csStr="abc";
        LPTSTR pStr=csStr.GetBuffer(10);
        strcpy(pStr,"12345");
        cout<<csStr.GetLength();       //3(错误的用法)
        csStr.ReleaseBuffer();
        cout<<csStr.GetLength();       //5(正确)
        pStr=NULL;
    //CString对象的任何方法都应在ReleaseBuffer之后调用

    21、LPTSTR GetBufferSetLength( int nNewLength );     申请新的空间,并返回指针

    例:csStr="abc";
        csStr.GetBufferSetLength(20);
        cout<<csStr;                  //abc
        count<<csStr.GetLength();     //20;
        csStr.ReleaseBuffer();
        count<<csStr.GetLength();     //3;
    //使用GetBufferSetLength后可以不必使用ReleaseBuffer。

     22、int FindOneOf( LPCTSTR lpszCharSet ) const;    查找lpszCharSet中任意一个字符在CString对象中的匹配位置。未找到时返回-1,否则返回字串的开始位置

    CString SpanExcluding( LPCTSTR lpszCharSet ) const;   返回对象中与lpszCharSet中任意匹配的第一个字符之前的子串

    例:csStr="abcdef";

        cout<<csStr.SpanExcluding("cf");    //ab

    CString SpanIncluding( LPCTSTR lpszCharSet ) const;    从对象中查找与lpszCharSe中任意字符不匹配的字符,并返回第一个不匹配字符之前的字串

    例:csStr="abcdef";
        cout<<csStr.SpanIncluding("fdcba");    //abcd

    (x) 转换成 LPCTSTR: LPCTSTR(cstring)

  • 相关阅读:
    Android H5混合开发(5):封装Cordova View, 让Fragment、弹框、Activity自由使用Cordova
    机器学习 AI 谷歌ML Kit 与苹果Core ML
    Android 设备唯一标识(多种实现方案)
    Android自定义控件:图形报表的实现(折线图、曲线图、动态曲线图)(View与SurfaceView分别实现图表控件)
    Android 禁止截屏、录屏 — 解决PopupWindow无法禁止录屏问题
    Android原生PDF功能实现:PDF阅读、PDF页面跳转、PDF手势伸缩、PDF目录树、PDF预览缩略图
    某助手验证码通信交互包分析
    IOS弓箭传说的插件开发
    微店APP协议简要分析
    Owhat sign参数分析
  • 原文地址:https://www.cnblogs.com/wllwqdeai/p/10296376.html
Copyright © 2011-2022 走看看