以下是一个派生类,用于提取'\n'分隔的各个子字符串,可以作为参考 提供几个主要的成员函数。 StringTokenizer::StringTokenizer(CString str,CString sep=" ") { index=0; count=0; CString str1=""; for(int i=0;i<str.GetLength() && sep.GetLength()==1;i++) { if(str.GetAt(i)==sep.GetAt(0)) { elements.Add(str1); str1=""; } else { str1+=str.GetAt(i); } } elements.Add(str1); count=elements.GetSize (); } // Method is used to fetch the tokens. CString StringTokenizer::getNextElement() { index++; if(index==count) { throw CString("Index out of Bounds"); } return elements.GetAt(index-1); } //method used to fetch the count of tokens from the string int StringTokenizer::countElements() { return count; } //fetch the elements at given position CString StringTokenizer::elementAt(int index) { if(index>=count ||index<0) { throw CString("Index out of Bounds"); } else return elements.GetAt(index); }
CSTring str;
str=str+"dsfasdfa";
str=str+"\n";
str=str+"dafdasfdadf";
行不?
str=str+"dsfasdfa";
str=str+"\n";
str=str+"dafdasfdadf";这两者效果是一样的,试试就知道了.
看看就会了
str+="\r\n"; // 注意这里是 \r\n 不是 \n
str+="lfdgf";ok
提供几个主要的成员函数。
StringTokenizer::StringTokenizer(CString str,CString sep=" ")
{
index=0;
count=0;
CString str1="";
for(int i=0;i<str.GetLength() && sep.GetLength()==1;i++)
{
if(str.GetAt(i)==sep.GetAt(0))
{
elements.Add(str1);
str1="";
}
else
{
str1+=str.GetAt(i);
}
}
elements.Add(str1);
count=elements.GetSize ();
} // Method is used to fetch the tokens.
CString StringTokenizer::getNextElement()
{ index++;
if(index==count)
{
throw CString("Index out of Bounds");
}
return elements.GetAt(index-1);
}
//method used to fetch the count of tokens from the string
int StringTokenizer::countElements()
{
return count;
}
//fetch the elements at given position
CString StringTokenizer::elementAt(int index)
{
if(index>=count ||index<0)
{
throw CString("Index out of Bounds");
}
else
return elements.GetAt(index);
}
可以啦
多谢大家
马上揭帖