您的位置:首页 > 其它

字符串截取以及字符数组和字符指针的区别

2015-05-28 10:44 309 查看
今天早晨在研究字符串截取时,查阅了很多资料。结果自己吧自己搞糊涂了。网上一些结论不一定都是正确的。
我在这里写出一些我自己的感悟,如果有不对的地方,真的真的希望大家赶紧帮我指正出来,万分感谢!!!

回到正题,首先先说一下字符串截取,毕竟万事都有个头儿嘛,盐打哪咸,醋打哪酸。纠结一早晨,还是因为字符串截取引发的血案。

string s = "1+2+6";

要求将+去掉,并且截取出其他的字符串。s 应该截取为1,2,6。

在网上查找资料,这是引用网址http://blog.csdn.net/chichigan1314/article/details/8487982#comments

发现这个比较全。

尝试第一种方法

<span style="font-family:SimSun;font-size:18px;">    char s[] = "1+2+6";
    char* delim = "+";
    char* p;
    char* str = strtok(s,delim);
    log("%s",str);
    while ((p = strtok(NULL,delim))) {
        log("%s",p);
    }</span>

但是这种方法要求原字符串必须是 char [] 这就比较纠结。因为这是c函数,c里面没有string,所以不用string类型我还能理解。但是我想用char* s = “1+2+6”; 这样也不行。想进strtok里面看看也进不去。于是我就想字符数组和字符指针到底有什么不同?

于是我又查找资料,这是引用网址

http://blog.csdn.net/johnny710vip/article/details/6725637

http://c.biancheng.net/cpp/html/81.html

http://www.oschina.net/question/103999_36888

自己又尝试代码调试

<span style="font-family:SimSun;font-size:18px;">#include <iostream>

using namespace std;

void main()
{
	char* p1 = "1+2+6";
	char p2[] = "1+2+6";
	cout<<p1<<endl;
	cout<<p2<<endl;
	cout<<*p1<<endl;
	cout<<*p2<<endl;
	cout<<p1[2]<<endl;
	cout<<p2[2]<<endl;
	//p1[2]='c';
	p2[2]='c';
	cout<<p1<<endl;
	cout<<p2<<endl;
	cout<<*p1<<endl;
	cout<<*p2<<endl;

	system("pause");
}</span>

这是输出结果:

1+2+6

1+2+6

1

1

2

2

1+2+6

1+c+6

1

1

请按任意键继续. . .

总结如下:

数组:

1、char temp[] = "123456"; temp是数组首地址,*temp 就是 temp[0]。

2、数组中的元素可以修改值。

3、数组必须付给初始值。

指针:

1、char * temp = "123456";temp是一块内存空间,可以不付给初始值。temp是字符串的首地址。*temp 就是字符串的第一个字符。

2、字符串中的元素不可以修改值。

所以调试之后发现基本没啥收获,只是肯定了一下以前的知识。



那么,再回到字符串截取这个事儿上。由于方法一只能用char[]。这让我很不爽,所以不用了。

再说boost方法,说实话,本人是菜鸟,第一次听说boost,感觉高大上,但是发现项目中需要引入boost库,下载有太麻烦。所以也不用了。

最后用标准模板库的两个函数实现了要求。

代码和第一个网址上的一样。但还是复制如下:



<span style="font-family:SimSun;font-size:18px;">#include <iostream>
#include <vector>
using namespace std;
//字符串分割函数
std::vector<std::string> split(std::string str,std::string pattern);
void main()
{
	string s = "1+2+6";
	cout<<s.c_str()<<endl;
	string delim = "+";
	vector<string> temp = split(s,delim);
	for (size_t i=0;i<temp.size();++i)
	{
		string a = temp.at(i);
		cout<<a.c_str()<<endl;
	}

	system("pause");
}

std::vector<std::string> split(std::string str,std::string pattern)
{
	std::string::size_type pos;
	std::vector<std::string> result;
	str+=pattern;
	//扩展字符串以方便操作
	size_t size=str.size();
	for(size_t i=0; i<size; i++)
	{
		pos=str.find(pattern,i);
		if(pos<size)
		{
			std::string s=str.substr(i,pos-i);
			result.push_back(s);
			i=pos+pattern.size()-1;
		}
	}
	return result;</span>

运行结果如下:

1+2+6

1

2

6

请按任意键继续. . .

这里用的主要是 find 和 substr 这两个函数,下面这段是摘抄过来的。我就当是写笔记了,所以见谅。

1、find函数

原型:size_t find ( const string& str, size_t pos = 0 ) const;

功能:查找子字符串第一次出现的位置。

参数说明:str为子字符串,pos为初始查找位置。

返回值:找到的话返回第一次出现的位置,否则返回string::npos

2、substr函数

原型:string substr ( size_t pos = 0, size_t n = npos ) const;

功能:获得子字符串。

参数说明:pos为起始位置(默认为0),n为结束位置(默认为npos)

返回值:子字符串

另外,size_t是一个 与 系统 相关 的 c标准库 中的 数据类型。和 int 相似。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: