VC字符串处理整理
场景:
1.在存储数据时有时接口需要合并字符串值,并以某些特殊字符来合并部分,到需要的时候再分割它。如一些数值,人名等。
2.C++有strtok,stringstream和find函数来实现分割。可以根据情况调用。
#include <stdlib.h>
#include <string.h>
#include
#include <iostream>
#include <sstream>
#include <vector>
using namespace std; void TestStrtok()
{
//1.非线程安全的,如果多个线程同时调用,会覆盖掉原来的值.
//2.支持以字符串分割.
//3.源字符串必须是可修改的.
char c_str[]="google||twitter||facebook||microsoft||apple||ibm||";
const char* delim = "||";
char* result = strtok(c_str,delim);
while(result != NULL)
{
cout << result << endl;
result = strtok(NULL,delim);
}
} void TestGetLineWithStringStream()
{
//1.线程安全的,但是只能以字符作为分隔符
stringstream ss("google|twitter|facebook|microsoft|apple|ibm|");
string str;
while(getline(ss,str,'|'))
{
cout << str << endl;
}
} void TestStringFind()
{
//1.自己实现,线程安全,支持字符串作为分隔符.缺点可能就是代码量多.
string str = "google||twitter||facebook||microsoft||apple||ibm||";
const char* delim = "||";
const int len = strlen(delim);
size_t index = ;
size_t pos = str.find(delim,index);
while(pos != string::npos)
{
string ss = str.substr(index,pos-index);
cout << ss << endl;
index = pos+len;
pos = str.find(delim,index);
} //cout << "is last?" << " index:" << index << " str.length():" << str.length() << endl;
if((index+) < str.length())
{
string ss = str.substr(index,str.length() - index);
cout << ss << endl;
}
} int main(int argc, char const *argv[])
{
cout << "TestStrtok: " << endl;
TestStrtok();
cout << "TestGetLineWithStringStream: " << endl;
TestGetLineWithStringStream();
cout << "TestStringFind: " << endl;
TestStringFind(); return ;
}
输出:
TestStrtok: googletwitterfacebookmicrosoftappleibmTestGetLineWithStringStream: googletwitterfacebookmicrosoftappleibmTestStringFind: googletwitterfacebookmicrosoftappleibm[Finished in 0.2s]char* a[3];
char* buf ="这是第一行\n这是第二行\n这是第三行\n"; 我想要用'\n'符将buf分割成三段并分别存入a[1],a[2],a[3]中,
请问该怎么做~
#include <stdio.h>
#include <string.h>
#include <malloc.h> int main()
{
char *a[];
char *buf ="这是第一行\n这是第二行\n这是第三行\n";
char *t, *pre = buf;
int i = , l; while (t = strchr(pre, '\n'))
{
if (i >= )
break; l = t - pre;
a[i] = (char *)malloc(l + );
strncpy(a[i], pre, l);
a[i][l] = '\0';
++i;
pre = t + ;
} for (i = ; i < ; ++i)
{
printf("%s\n", a[i]);
free(a[i]);
} return ;
}
VC字符串处理整理的更多相关文章
- vc字符串转换处理:(绝对精华,收集所有的例子)
vc字符串转换处理:(绝对精华,收集所有的例子) 1.头文件中要定义宏; #define UNICODE #define _UNICODE //////////// ...
- Lua字符串库(整理)
Lua字符串库小集 1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回 ...
- Char型和string型字符串比较整理
1.赋值 char赋值: char ch1[] = "give me"; char ch2[] = "a cup"; strcpy(ch1,ch2); cout ...
- vim 字符串替换整理
公司项目测试,要在vi编辑其中进行多路径修改,这时候用到了字符串替换的知识,在这里我自己整理了一下. 一.基本内容替换,无特殊符号 :s/old/new/ 替换当前行第一个 old 为 new ...
- C和C++字符串处理整理
在刷leetcode题目的过程中,发现自己对于c和c++字符串的处理并不是很拿手,处理起来比较费劲,而且,算法题似乎很中意字符串的处理,有很多题目都涉及到它.字符串处理比较基础,但是很重要,因此,整理 ...
- C/C++字符串使用整理
在C语言中,字符串有多种操作与处理方法.话不多说,下面就整理一下C语言中字符串的使用整理. 1.头文件 字符串的头文件: #include<cstring> 2.输入 通常,字符串有多种输 ...
- VC字符串转换常用函数
最近在做一些关于VC的ActiveX小插件,经常会遇到字符串处理的问题,狂查CSDN和MSDN,结果并不理想.先说明一下,相关处理函数在VC++6.00测试通过.也许很多人不能理解,现在都什么年代了, ...
- VC++字符串的使用及转换
CString ,BSTR ,LPCTSTR之间关系和区别 CString是一个动态TCHAR数组,BSTR是一种专有格式的字符串(需要用系统提供的函数来操纵,LPCTSTR只是一个常量的TCHAR指 ...
- VC++ 字符串操作学习总结
vc++中各种字符串(转载) http://www.cnblogs.com/tomin/archive/2008/12/28/1364097.html CString ,BSTR ,LPCTSTR之间 ...
随机推荐
- Ubuntu14.04下codeblocks手动编译配置bost_1_57_0
环境:ubuntu 14.04 32bit,boost_1_57_0 前期准备:boost中,用到了别的函数库,所以为了使用boost中相应的功能,需要先安装系统中可能缺失的库 apt-get in ...
- direct path read/write (直接路径读/写)
转载:http://www.dbtan.com/2010/04/direct-path-readwrite.html direct path read/write (直接路径读/写): 直接路径读(d ...
- 信息学奥赛(NOIP)复赛学习方法推荐
一.确定你的语言 NOIP包括三种语言c/c++/pascal,在最初必须确定自己使用的语言.没有c/c++基础的,个人建议使用pascal,因为它更容易上手,如果有充裕的时间,则建议c/c++,因为 ...
- bash下. : () {} [] [[]] (())的解释
bash下有很多像{}.[]等一些符号命令,下面是我对一些常用的符号命令的学习笔记,若有错误或纰漏望各位兄弟指正. 一..(source).(点)与source命令一样,从文件中读取并执行命令,无论该 ...
- python+selenium:iframe框架中多种定位
方法一:通过索引,id,name,WebElement定位 from selenium import webdriverdriver = webdriver.Firefox()driver.switc ...
- python入门-IF语句
1 格式 cars = ['audi','bmw','subaru','toyata'] for car in cars: if car =='bmw': print(car.upper()) els ...
- python入门-列表
列表使用[]来标识 列表和PHP中的数组类似 包括使用和访问方式都是类似 可以用下标索引的方式直接访问 来几个例子,这样看起来才舒服 names = ['baker','pitty','david', ...
- django 使用多个数据库
在django项目中, 一个工程中存在多个APP应用很常见. 有时候希望不同的APP连接不同的数据库,这个时候需要建立多个数据库连接. 参考:http://blog.csdn.net/songfree ...
- delphi RAD XE 安装路径 重装备份
重装的时候,不要删除c盘C:\ProgramData下的guid目录.以便完整卸载旧版本. 控件安装的生成的目标文件路径 C:\Users\Public\Documents\Embarcadero\S ...
- MyEclipse: Java代码与UML自动转换
第一步:新建UML2 第二步:拖拽左边的代码向右侧