一个比较麻烦的字符串问题

bit_hj 2003-08-20 11:37:25
请问怎样将这个字符串分开。
a.添加;b.修改;c.删除;d.查看
他们是以;分开的
请问怎样可以将这个长字符串分开(以;分开)
分成
a.添加
b.修改
c.删除
d.查看
这四个子字符串。
字串个数不一定是四个。
...全文
64 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yydy 2003-08-20
  • 打赏
  • 举报
回复
:)
freshman2003 2003-08-20
  • 打赏
  • 举报
回复
哈哈,被人抢了行。
freshman2003 2003-08-20
  • 打赏
  • 举报
回复
用AnsiPos()取得“;”的位置,用SubString()提取子串。
free1949 2003-08-20
  • 打赏
  • 举报
回复
string Source("a.添加;b.修改;c.删除;d.查看;.....");
string temp("");
int Pos=Source.Pos(";");//获得第一个“;”位置
if(Pos!=0) //是否存在分隔符号“;”
{
do
{
temp=Source.SubString(1,Pos);//得到分开的串
Source.delete(1,Pos+1);//删除已取出的串和“;”
Pos=Source.Pos(";"); //重新获得第一个“;”位置
}while(Pos!=0)
}
江山易改 2003-08-20
  • 打赏
  • 举报
回复
你是要分成四个字符串吗?
zjqyb 2003-08-20
  • 打赏
  • 举报
回复
ExtractString
Santos 2003-08-20
  • 打赏
  • 举报
回复
用TStringList吧,它缺省的分隔字符为空格或逗号,所以你只要将"a.添加;b.修改;c.删除;d.查看"改为"a.添加 b.修改 c.删除 d.查看"或"a.添加,b.修改,c.删除,d.查看"即可。
TStringList要这样用:

TStringList *MyStringList = new TStringList();

try
{
MyStringList->CommaText = "a.添加, b.修改, c.删除, d.查看";
}
__finally
{
delete MyStringList;
}
Caption = MyStringList->Count; //-- 此处Count值为4

祝你好运!
kmfangxun 2003-08-20
  • 打赏
  • 举报
回复
建议用标准函数 strtok(char*,";");头文件:stdlib.h;帮助上有例.
lvjack 2003-08-20
  • 打赏
  • 举报
回复
TStringList *Strs= new TStringList;
Strs->Delimiter = ';';
Strs->DelimitedText = "a.添加;b.修改;c.删除;d.查看";
ShowMessage(IntToStr(Strs->Count));
for (int i=0;i<Strs->Count;i++)
{
ShowMessage(Strs->Strings[i]);
}
或者(代码不是我写的)

//用标准库的vector和string


#include <vector>
#include <string>
#include <iostream>
using namespace std;

vector<string> *split(string& sLine, char flag)
{
vector<string> *psvect=new vector<string>;//string数组的指针

string::size_type pos=0,prev_pos=0;
while( (pos=sLine.find_first_of(flag,pos))!=string::npos)
{
psvect->push_back(sLine.substr(prev_pos,pos-prev_pos));
prev_pos = ++pos;
}

psvect->push_back(sLine.substr(prev_pos,pos-prev_pos));//处理最后一段


return psvect;

}


int main(int argc, char* argv[])
{
string s="2003-7-1";
vector<string> *psvect; //string数组的指针
psvect=split(s,'-');
for( unsigned int i=0; i<psvect->size(); i++)
{
cout<< (*psvect)[i]<<endl;
}
delete psvect;
cin.get();
return 0;
}
//---------------------------------------------------------------------------

windlyzhang 2003-08-20
  • 打赏
  • 举报
回复
AnsiString Source="a.添加;b.修改;c.删除;d.查看;";

String str1=SubStr(Source);//str1=a.添加
String str2=SubStr(Source);//str2=ab.修改
String str3=SubStr(Source);//str3=c.删除
String Str4=SubStr(Source);//str4=d.查看


String __fastcall TFrmField::SubStr(String &Source,String Des)
{
int pos=Source.Pos(Des);
if(pos==0) return Source;

int len=Source.Length();

String SubString=Trim(Source.SubString(1,pos-1));
Source=Trim(Source.SubString(pos+1,len-pos));
return SubString;
}
Cocoky 2003-08-20
  • 打赏
  • 举报
回复
这样行
这样不知道行不行:
AnsiString Source="a.添加;b.修改;c.删除;d.查看;.....";
AnsiString temp[MAX];
int tempcount = 0;
char *chr;
chr = Source.c_str();
for(int i=0, tempcount = 0;i<Source.Length();i++)
{
if(chr[i]!=';')
{
temp[tempcount] += chr[i];
}
else
{
tempcount ++;
}

}

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧