字符串用法集合
来源:互联网 发布:windows iso怎么下载 编辑:程序博客网 时间:2024/06/06 00:20
1查找
- int Find( TCHAR ch ) const;
- int Find( LPCTSTR lpszSub ) const;
- int Find( TCHAR ch, int nStart ) const;
- 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)。
示例:
- //下面演示第一个例子
- // 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)
//下面演示第一个例子// 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函数将不同格式的数据合并的方法
- 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);
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
- int iIndex = strFileName.ReverseFind(_T(‘\\’)); //从字符串末尾处开始查找返回字符所在的索引,也可以用Find
int iIndex = strFileName.ReverseFind(_T('\\')); //从字符串末尾处开始查找返回字符所在的索引,也可以用Find
4 截取字符串
- strFileName = strFileName.Right(strFileName.GetLength() - iIndex - 1);//从右边开始取参数个字符,也可以用Left
strFileName = strFileName.Right(strFileName.GetLength() - iIndex - 1);//从右边开始取参数个字符,也可以用Left
- //截取指定个数的字符串可以用mid
//截取指定个数的字符串可以用mid
5 判断字符串是否全为数字
- 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返回给该判断函数 */
- }
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 判断字符串是否都是整数
- int iLength = 0;
- CString strTemp(str);
- iLength = strTemp.GetLength();
- for(int i = 0; i < iLength; i++)
- {
- if(str[i] == ‘.’)
- return false;
- }
int iLength = 0;CString strTemp(str);iLength = strTemp.GetLength();for(int i = 0; i < iLength; i++){ if(str[i] == '.') return false;}
7 保留小数点后三位输出
- strTemp.Format(_T(“%.3f”),m_dShiftUnit);
strTemp.Format(_T("%.3f"),m_dShiftUnit);
8 多行书写字符串
- cout<<“11”
- ”22”
- ”33”;
- //或
- cout<<”11\
- 22\
- 33”;
- //相当于
- cout<<”112233”;
cout<<"11" "22" "33";//或cout<<"11\ 22\ 33";//相当于cout<<"112233";
9 字符串转化为整数或浮点数
atoi
atof
10 CString 转 char*
- CString str = “aaaaaaaaaaaaaaaaaaa”;
- char *pTemp = (LPSTR)(LPCTSTR)str;
CString str = "aaaaaaaaaaaaaaaaaaa";char *pTemp = (LPSTR)(LPCTSTR)str;
11 将字符串转换为数字
- double dDensity = _wtof(sItemText);
double dDensity = _wtof(sItemText);
12 函数要求的参数为LPCWSTR参数的处理方法
- TCHAR szDir[256];
- DWORD dwBufLen = 256;
- szDir[0] = _T(’\0’);
- CString sPath;
- GetCurrentDirectory(dwBufLen, szDir);//szDir即可作为LPCWSTR参数
- 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
- 字符串用法集合
- String字符串用法集合
- C#字符串用法集合
- 字符串用法集合
- ibatis 迭代中使用in传递字符串数组或集合时的用法
- 字符串用法
- 数据库连接字符串集合
- 数据库连接字符串集合
- 操作集合和字符串
- 数据库连接字符串集合
- 数据库连接字符串集合
- C# 数据库连接字符串集合
- C#数据库连接字符串集合
- C# 数据库连接字符串集合
- C# 数据库连接字符串集合
- C# 数据库连接字符串集合
- 字符串笔试题集合
- c#数据库连接字符串集合
- 同一台Ubuntu 启动多个mysql
- 先码一个regular expression
- 解决丢失api-ms-win-crt-runtime-|1-1-0.dll的问题
- 高斯滤波
- js中cookie的使用详细分析
- 字符串用法集合
- 一个简单的自定义View,可以显示上图下字
- Extjs 将store等数组格式传入后台计算
- CRC码
- stm8 ADC中断采集
- js 实现用div 上下分屏
- C++ 中引用、指针、const
- UE4 声音
- Error response from daemon: Cannot start container System error: exec: "/bin/bash": stat /bin/bash: