Cstring的使用

https://msdn.microsoft.com/zh-cn/aa315043

1、字符串提取函数,CString::Left、CString::Mid 、CString::Right

CString::Mid

CString Mid( int nFirst ) const; throw( CMemoryException );

CString Mid( int nFirst, int nCount ) const; throw( CMemoryException );

Return Value

A CString object that contains a copy of the specified range of characters. Note that the returned CString object may be empty.

Parameters

nFirst

The zero-based index of the first character in this CString object that is to be included in the extracted substring.

nCount

The number of characters to extract from this CString object. If this parameter is not supplied, then the remainder of the string is extracted.

Remarks

Extracts a substring of length nCount characters from this CString object, starting at position nFirst (zero-based). The function returns a copy of the extracted substring. Mid is similar to the Basic MID$ function (except that indexes are zero-based).

For multibyte character sets (MBCS), nCount refers to each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters.

Example

The following example demonstrates the use of CString::Mid.

// example for CString::Mid
CString s( _T("abcdef") );
ASSERT( s.Mid( 2, 3 ) == _T("cde") );

CString::Left

CString Left( int nCount ) const; throw( CMemoryException );

Return Value

A CString object containing a copy of the specified range of characters. Note that the returned CString object may be empty.

Parameters

nCount

The number of characters to extract from this CString object.

Remarks

Extracts the first (that is, leftmost) nCount characters from this CString object and returns a copy of the extracted substring. If nCount exceeds the string length, then the entire string is extracted. Left is similar to the Basic LEFT$ function (except that indexes are zero-based).

For multibyte character sets (MBCS), nCount refers to each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters.

Example

The following example demonstrates the use of CString::Left.

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

CString::Right

CString::Right

CString Right( int nCount ) const; throw( CMemoryException );

Return Value

A CString object that contains a copy of the specified range of characters. Note that the returned CString object may be empty.

Parameters

nCount

The number of characters to extract from this CString object.

Remarks

Extracts the last (that is, rightmost) nCount characters from this CString object and returns a copy of the extracted substring. If nCount exceeds the string length, then the entire string is extracted. Right is similar to the Basic RIGHT$ function (except that indexes are zero-based).

For multibyte character sets (MBCS), nCount refers to each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters.

Example

The following example demonstrates the use of CString::Right.

// example for CString::Right
CString s( _T("abcdef") );
ASSERT( s.Right(2) == _T("ef") );
 
时间: 2024-12-22 19:12:44

Cstring的使用的相关文章

C++中Cstring、wstring 和string互相转换总结

通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操作类.wstring是操作宽字符串的类..CString是对string(字符串)和wstring(宽字符串)的一个封装,常用在mfc中,用来解决编码问题的.在编程过程中,经常会遇到Cstring.wstring 和string之间的相互转换,在这里做了个简单地总结,另外也会附上其他类型的转换.常见的转换方式

使用sendto发送CString类型数据

发送端: int len = m_strSend.GetLength(); if (0 == len) { strState.Format(L"请填写需要发送的数据!"); GetDlgItem(IDC_STATIC_SEND_STATE)->SetWindowText(strState); //动态指定static text显示内容 return; } int byteLenSend = (len + 1)*sizeof(wchar_t); USES_CONVERSION;//

String,CString,TCHAR,char之间区别和联系

char是类型TCHAR也是!不过他可以通过是否定义了UNICODE宏来判断到底是char还是w_char; TCHAR是一种字符串类型,它让你在以MBCS和UNNICODE来build程序时可以使用同样的代码,不需要使用繁琐的宏定义来包含你的代码,而char代表ASCII的字符 #ifdef UNICODE   typedef wchar_t TCHAR;   #else   typedef char TCHAR;   #endif 所以用MBCS来build时,TCHAR是char,使用UN

unicode下char*和CString

1.对话框打印char* char* info=""; ::MessageBoxA(this->m_hWnd, info, "", MB_OK); 2.CString转char* int nLen; char * wsabuf = NULL;#ifdef _UNICODE //CString转换成char* USES_CONVERSION; wsabuf = W2A(send_txt_str);//send_txt_str为CString消息#else#end

CString中Format函数与格式输入与输出

CString中Format函数与格式输入与输出 Format是一个很常用,却又似乎很烦的方法,以下是它的完整概貌,以供大家查询之用: 格式化字符串forma("%d",12)意思是将一个整形的格式化的字符(我认为是保持其形状不变) 1).格式说明总是以%字符开始,以下是不同类型数据的格式方式%号后的说明: d输出带符号十进制数 o输出无符号八进制数 x输出无符号十六进制数 u输出无符号数 c输出单个字符 s输出一串字符 f输出实数(6位小数) e以指数形式输出实数 g选用f与e格式中

CString、std::string格式化字符串

=============================CString================================== 当有多个字串时,比如     int   n1   =   5;     int   n2   =   10;     char   sz1[]   =   "abcdefg";     char   sz2[]   =   "hijklmn";         用std中的string如何写出最简单的代码得到MFC中CStr

关于CString 类的初步实现

耗费了2个小时重写了一遍CString 若有漏洞 欢迎指出~ 谢谢 首先贴出代码部分: //////////////////////////////////////////////////////////////// ////////////////////////XNString.h//////////////////////////// //////////////////////////////////////////////////////////////// #pragma oncet

MFC中如何将16进制字符转化成10进制,包括CString与char*的转换

1 CString m_str; 2 m_str="1F"; //16进制字符为1F 3 m_str = "0x"+m_str; 4 char *p= (char*)((LPCTSTR)m_str); //将CString的字符m_str转化成char*型,并赋值给指针p 5 char *str; 6 int m_Speed = (int)strtol(p, &str, 16); //十六进制转化成10进制,并赋值给整形数据m_Speed

std::string 和 CString问题

std::string stdTemp; CString strTemp; strTemp = stdTemp;    ;//这一步直接赋值可不可以 因为CString可以接受const char*的赋值,而且std::string有个返回const char*的方法,c_str(),所以,应该这样写: strTemp = stdTemp.c_str();

CIPAddress中获取的IP地址与CString的互相转换

// IPAddressCtrl中的IP地址转为CString CIPAddressCtrl* pIp = (CIPAddressCtrl*)GetDlgItem(IPADDRESS); BYTE bytIp1, bytIp2, bytIp3, bytIp4; pIp->GetAddress(bytIp1, bytIp2, bytIp3, bytIp4); CString strIp; strIp.Format(_T("%u.%u.%u.%u"), bytIp1, bytIp2,