1、格式化字符串

CString s;
s.Format(_T("The num is %d."), i);相当于sprintf()

2、转为 int

转10进制最好用_ttoi(),它在 ANSI 编码系统中被编译成_atoi(),而在 Unicode 编码系统中编译成_wtoi()。用_tcstoul()或者_tcstol()可以把字符串转化成任意进制的(无符号/有符号)长整数。

CString hex = _T("FAB");
CString decimal = _T("4011");
ASSERT(_tcstoul(hex, 0, 16) == _ttoi(decimal));

3、转为 char *

3.1 强制类型转换为 LPCTSTR,不能修改字符串

LPCTSTR p = s; 或者直接 (LPCTSTR)s;

3.2 使用 GetBuffer 方法

不给 GetBuffer 传递参数时它使用默认值 0,意思是:“给我这个字符串的指针,我保证不加长它”。假设你想增加字符串的长度,就必须将你需要的字符空间大小(注意:是字符而不是字节,因为 CString 是以隐含方式感知 Unicode 的)传给它。当调用 ReleaseBuffer 时,字符串的实际长度会被重新计算,然后存入 CString 对象中。
必须强调一点,在 GetBuffer 和 ReleaseBuffer 之间这个范围,一定不能使用你要操作的这个缓冲的 CString 对象的任何方法。因为 ReleaseBuffer 被调用之前,该 CString 对象的完整性得不到保障。

LPTSTR p = s.GetBuffer();
// do something with p
int m = s.GetLength(); // 可能出错!!!
s.ReleaseBuffer();
int n = s.GetLength(); // 保证正确

4、分割字符串

4.1 单字符分割

AfxExtractSubString(CString& rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '\n');

CString csFullString(_T("abcd-efg-hijk-lmn"));
CString csTemp;
AfxExtractSubString(csTemp, (LPCTSTR)csFullString, 0, '-'); // 得到 abcd
AfxExtractSubString(csTemp, (LPCTSTR)csFullString, 1, '-'); // 得到 efg
AfxExtractSubString(csTemp, (LPCTSTR)csFullString, 2, '-'); // 得到 hijk
AfxExtractSubString(csTemp, (LPCTSTR)csFullString, 3, '-'); // 得到 lmn

分隔符可以随便指定:
AfxExtractSubString(csTemp, (LPCTSTR)csFullString, 0, 'f'); // 得到 abcd-e

4.2 字符串分割

利用CString的Find方法,然后再组成数组。

函数:

Split(CString source; CStringArray& dest ; CString division )

参数含义:

CString source--------------需要截取的原字符串,

CStringArray& dest ---------最终结果的数组

CString division -------------用来做分割符的字符串

void Split(CString source, CStringArray& dest, CString division)
{
    dest.RemoveAll();
    int pos = 0;
    int pre_pos = 0;
    while( -1 != pos ){
        pre_pos = pos;
        pos = source.Find(division,(pos+1));
        dest.Add(source.Mid(pre_pos,(pos-pre_pos)));
    }
}

CString 字符串转化和分割的更多相关文章

  1. VC 字符串转化和分割

    原文:点击这里. 备忘:为了适用于Unicode环境,要养成使用_T()宏的习惯 1.格式化字符串 CString s;s.Format(_T("The num is %d."), ...

  2. PL/SQL中字符串变量的分割转化

    在编写PL/SQL时,有时候我们需要处理这样一个输入的变量,它的格式是由多个值通过分隔符组成的字符串,如“1,2,3”,我们需要将这个变量加入到我们的SQL中,形成诸如in('1','2','3')的 ...

  3. OC 数组以及字符串拼接与分割

    //@""空的字符串对象-------分割 NSString * ptr = @"I am a man"; NSArray * array = [ptr com ...

  4. 笔记:iOS字符串的各种用法(字符串插入、字符串覆盖、字符串截取、分割字符串)(别人的代码直接复制过来的,我脸皮有点厚)

    NSString* str=@"hello";//存在代码区,不可变 NSLog(@"%@",str); //1.[字符串插入] NSMutableString ...

  5. Obj-C数组以及字符串拼接与分割

    本文转载至 http://mobile.51cto.com/iphone-392148.htm Obj-C只是增加了一点“特殊语料”的C语言,所以可以用printf()代替NSLog().但我们建议使 ...

  6. iOS字符串的各种用法(字符串插入、字符串覆盖、字符串截取、分割字符串)

    NSString* str=@"hello";//存在代码区,不可变 NSLog(@"%@",str); //1.[字符串插入] NSMutableString ...

  7. NSString用法,object-C数组以及字符串拼接和分割

    一.介绍使用NSString创建一个字符串的代码如下: #import <Foundation/Foundation.h>int main (int argc, char *argv[]) ...

  8. 通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母

    通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母 例如 我的中国心    ==> wdzgx 我的中国心ya ==> wdzgxya woai我的中国 ==> w ...

  9. PHP基础语法: echo,var_dump, 常用函数:随机数:拆分字符串:explode()、rand()、日期时间:time()、字符串转化为时间戳:strtotime()可变参数的函数:PHP里数组长度表示方法:count($attr[指数组]);字符串长度:strlen($a)

    PHP语言原理:先把代码显示在源代码中,再通过浏览器解析在网页上 a. 1.substr;  //用于输出字符串中,需要的某一部分 <?PHP $a="learn php"; ...

随机推荐

  1. 扩展编写jquery插件的方法

    比如要扩展验证功能(jquery.validate.js)中的 messages: { required: "This field is required.", remote: & ...

  2. HTML元素遮挡Flash之梦

    wmode参数: transparent模式:可用z-index控制层级 opaque模式:可用z-index控制层级 window模式:flash层级在浏览器核心显示窗口之上,flash会盖住与他重 ...

  3. socket学习及各类错误码(部分转)

    如果本地有多个网卡(即多个ip),要指定本地发送网卡,则在建立的socket上bind所指定的网卡进行connect和send操作.例子程序如下: #include <stdio.h>#i ...

  4. Qt编程之对QGraphicsItem点击右键弹出菜单

    就是对这个contextMenuEvent 事件重新实现,在这个事件函数中创建菜单,大概就是这样. void MyItem::contextMenuEvent(QGraphicsSceneContex ...

  5. EBS服务器管理

    EBS 服务的重启 停止Oracle Applications 服务的顺序为:停止应用产品相关服务,再停止数据库相关服务: 启动Oracle Applications 服务的顺序为:启动数据库相关服务 ...

  6. bzoj1676[Usaco2005 Feb]Feed Accounting 饲料计算

    Description Farmer John is trying to figure out when his last shipment of feed arrived. Starting wit ...

  7. 3D objects key rendering steps

    Key steps of Rendering objects: 1 Create objects’ meshes, which we can use C++’s vector container to ...

  8. 基本NT式驱动代码结构

    #include <ntddk.h> void DriverUnload(IN PDRIVER_OBJECT DriverObject);NTSTATUS MyCreateClose(IN ...

  9. 第28讲 UI组件之 ListView和ArrayAdapter

    第28讲 UI组件之 ListView和ArrayAdapter 1. Adapter 适配器 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的 ...

  10. eclipse指定启动时的jdk(xjl456852原创)

    在eclipse安装目录中找到eclipse.ini 在第一行配置(即可启动按指定版本的jdk启动eclipse): -vm D:\soft\Java\jre8\bin\server\jvm.dll ...