//----------------------MyString.h---------------
#ifndef MYSTRING_H_
#define MYSTRING_H_
#include<vector>
class myString
{
public:
myString(const char*);
~myString();
typedef std::vector<myString> myStringCollection;
const myStringCollection Split(const char separators[] );
private:
char* msg;
unsigned length;
};
#endif
//--------------------MyString.cpp---------------------------------
#include<string.h>
#include "MyString.h"
typedef std::vector<myString> StringCollection;
myString::myString(const char *value)
{
length = (unsigned)strlen(value);
msg = new char[length+1];
if(value!=NULL)
strcpy(msg,value);
}
myString::~myString()
{
length=0;
delete [] msg;
msg=NULL;
CharPointerCollection.clear();
}
const StringCollection myString::Split(const char separators[])
{
StringCollection Strings;
unsigned size = sizeof(separators);
if(size > 0)
{
//char* temp = msg;
char* temp = new char[length+1];
strcpy(temp,msg);
//char* current = msg;
char* current = temp;
//unsigned current = length;
for(unsigned cur=0;cur<length;cur++)
{
for( unsigned count = 0;count < size; count++)
{
if(*(temp + cur) != separators[count])
continue;
else
{
*(temp+cur) = '\0';
myString ms(current);
Strings.push_back(ms);//此处执行之后,ms.msg已经变了,很怪的字符。
current += cur;
}
}
}
delete [] temp;
temp = NULL;
current = NULL;
delete current;
return Strings;
}
else
{
Strings.push_back(*this);
return Strings;
}
}
const char* const myString::GetData() const
{
return msg;
}
//------------------------------------main.cpp-----------------------------------
#include "MyString.h"
#include<iostream>
using namespace std;
int main()
{
char p[] = {',','|','*','#'};
myString a("af,jfkd|fdjk*dsfg#fasdf"); //目的:把此字符串用分割符号分开。
cout<<a.Split(p)[0].GetData();
return 0;
}