字符串用法集合

来源:互联网 发布:windows iso怎么下载 编辑:程序博客网 时间:2024/06/06 00:20

1查找

[cpp] view plain copy
print?
  1. int Find( TCHAR ch ) const;  
  2. int Find( LPCTSTR lpszSub ) const;  
  3. int Find( TCHAR ch, int nStart ) const;  
  4. int Find( LPCTSTR lpszSub, int nStart ) const;  
int Find( TCHAR ch ) const;int Find( LPCTSTR lpszSub ) const;int Find( TCHAR ch, int nStart ) const;int Find( LPCTSTR lpszSub, int nStart ) const;
返回值
返回此CString对象中与需要的子字符串或字符匹配的第一个字符的从零开始的索引;如果没有找到子字符串或字符则返回-1。
参数: ch 要搜索的单个字符。  
lpszSub 要搜索的子字符串。  
nStart 字符串中开始搜索的字符的索引,如果是0,则是从头开始搜索。如果nStart不是0,则位于nStart处的字符不包括在搜索之内。  
pstr 指向要搜索的字符串的指针。  
说明
此成员函数用来在此字符串中搜索子字符串的第一个匹配的字符。函数的重载可以接收单个字符(类似于运行时函数strchr)和字符串(类似于strstr)。
示例
[cpp] view plain copy
print?
  1. //下面演示第一个例子  
  2. // CString::Find( TCHAR ch )  
  3. CString s( ”abcdef” );  
  4. ASSERT( s.Find( ’c’ ) == 2 );  
  5. ASSERT( s.Find( ”de” ) == 3 );  
  6. // 下面演示第二个例子  
  7. // CString::Find(TCHAR ch,int nStart)  
  8. CString str(”The stars are aligned”);  
  9. int n = str.Find(‘e’,5);  
  10. ASSERT(n == 12)  
//下面演示第一个例子// CString::Find( TCHAR ch )CString s( "abcdef" );ASSERT( s.Find( 'c' ) == 2 );ASSERT( s.Find( "de" ) == 3 );// 下面演示第二个例子// CString::Find(TCHAR ch,int nStart)CString str("The stars are aligned");int n = str.Find('e',5);ASSERT(n == 12)

2 Format函数将不同格式的数据合并的方法

[cpp] view plain copy
print?
  1. m_sReadString.Format(_T(“%s, 数据个数:%d\r\n”), buf, len);  
  2. char sFoundFileName[200];    
  3. strcpy(sFoundFileName,  m_sReadString.GetBuffer(200));   
  4. char sTempDir[200];    
  5. sprintf(sTempDir ,”%s\\%s”, sDirName, sFoundFileName);   
m_sReadString.Format(_T("%s, 数据个数:%d\r\n"), buf, len);char sFoundFileName[200];  strcpy(sFoundFileName,  m_sReadString.GetBuffer(200)); char sTempDir[200];  sprintf(sTempDir ,"%s\\%s", sDirName, sFoundFileName); 

3 反转查找ReverseFind

[cpp] view plain copy
print?
  1. int iIndex = strFileName.ReverseFind(_T(‘\\’)); //从字符串末尾处开始查找返回字符所在的索引,也可以用Find  
int iIndex = strFileName.ReverseFind(_T('\\')); //从字符串末尾处开始查找返回字符所在的索引,也可以用Find

4 截取字符串

[cpp] view plain copy
print?
  1. strFileName = strFileName.Right(strFileName.GetLength() - iIndex - 1);//从右边开始取参数个字符,也可以用Left  
strFileName = strFileName.Right(strFileName.GetLength() - iIndex - 1);//从右边开始取参数个字符,也可以用Left
[cpp] view plain copy
print?
  1. //截取指定个数的字符串可以用mid  
//截取指定个数的字符串可以用mid

5 判断字符串是否全为数字

[cpp] view plain copy
print?
  1. int check(char *s)    /* 定义一个判断是否全为数字的函数,其参数为字符串s  */  
  2. {  
  3.  int i,mark=0;        /* 定义用于循环的整型变量i和用于记录的整型变量mark  */  
  4.  for(i=0;s[i];i++)    /* 开始一个个判断,如果s[i]为空即结束判断  */  
  5.  {  
  6.   if((s[i]!=‘.’)&&((s[i]<48)||(s[i]>57))) mark++;   
  7.   /* 如果s[i]不是字符‘.’并且不是数字(即小于48或大于57),则让mark自身加1以作记录; */  
  8.   if(mark)   
  9.    i=10000;     
  10.   /* 如果mark不为0,则表示结果已揭晓,不用再判断了,让i等于10000以退出循环,你的字符串不会有10000个字符吧  */  
  11.  }  
  12.  if(mark)   
  13.   return 0:   
  14.  else   
  15.   return 1;   
  16.  /* 如果mark不为0,则表示该字符串不全是数字,将0返回给该判断函数,否则全是数字,将1返回给该判断函数  */  
  17. }  
int check(char *s)    /* 定义一个判断是否全为数字的函数,其参数为字符串s  */{ int i,mark=0;        /* 定义用于循环的整型变量i和用于记录的整型变量mark  */ for(i=0;s[i];i++)    /* 开始一个个判断,如果s[i]为空即结束判断  */ {  if((s[i]!='.')&&((s[i]<48)||(s[i]>57))) mark++;   /* 如果s[i]不是字符‘.’并且不是数字(即小于48或大于57),则让mark自身加1以作记录; */  if(mark)    i=10000;     /* 如果mark不为0,则表示结果已揭晓,不用再判断了,让i等于10000以退出循环,你的字符串不会有10000个字符吧  */ } if(mark)   return 0:  else   return 1;  /* 如果mark不为0,则表示该字符串不全是数字,将0返回给该判断函数,否则全是数字,将1返回给该判断函数  */}

6 判断字符串是否都是整数

[cpp] view plain copy
print?
  1. int iLength = 0;  
  2. CString strTemp(str);  
  3. iLength = strTemp.GetLength();  
  4. for(int i = 0; i < iLength; i++)  
  5. {  
  6.   if(str[i] == ‘.’)  
  7.   return false;  
  8. }  
int iLength = 0;CString strTemp(str);iLength = strTemp.GetLength();for(int i = 0; i < iLength; i++){  if(str[i] == '.')  return false;}

7 保留小数点后三位输出

[cpp] view plain copy
print?
  1. strTemp.Format(_T(“%.3f”),m_dShiftUnit);  
strTemp.Format(_T("%.3f"),m_dShiftUnit);

8 多行书写字符串

[cpp] view plain copy
print?
  1. cout<<“11”  
  2.       ”22”  
  3.       ”33”;  
  4. //或  
  5. cout<<”11\  
  6.       22\  
  7.       33”;  
  8. //相当于  
  9. cout<<”112233”;  
cout<<"11"      "22"      "33";//或cout<<"11\      22\      33";//相当于cout<<"112233";

9 字符串转化为整数或浮点数

atoi 
atof

10 CString 转 char*

[cpp] view plain copy
print?
  1. CString str = “aaaaaaaaaaaaaaaaaaa”;  
  2. char *pTemp = (LPSTR)(LPCTSTR)str;  
CString str = "aaaaaaaaaaaaaaaaaaa";char *pTemp = (LPSTR)(LPCTSTR)str;

11 将字符串转换为数字

[cpp] view plain copy
print?
  1. double dDensity = _wtof(sItemText);  
double dDensity = _wtof(sItemText);

12 函数要求的参数为LPCWSTR参数的处理方法

[cpp] view plain copy
print?
  1. TCHAR szDir[256];  
  2. DWORD dwBufLen = 256;  
  3. szDir[0] = _T(’\0’);  
  4. CString sPath;  
  5. GetCurrentDirectory(dwBufLen, szDir);//szDir即可作为LPCWSTR参数  
  6. sPath.Format(_T(”%s”), szDir);  
TCHAR szDir[256];DWORD dwBufLen = 256;szDir[0] = _T('\0');CString sPath;GetCurrentDirectory(dwBufLen, szDir);//szDir即可作为LPCWSTR参数sPath.Format(_T("%s"), szDir);

13 在字符串中显示%号

%%

未完待续…

0 0
原创粉丝点击