c++截取字符串

2025-04-13 08:23:55
推荐回答(3个)
回答(1):

#include
#include
using namespace std;

void display(const int *a)
{
for(int i=0;a[i];i++)
cout< cout<}

void cat(const string &str,int *temp,int &a)
{
int count=0,k=0;
unsigned int i=0;
char strtemp[255]=;
while(i {
while(isdigit(str[i])&&i strtemp[k++]=str[i++];
strtemp[k]='\0';
temp[count++]=atoi(strtemp);
strtemp[0]='\0';
k=0;
i++;
}
temp[count]='\0';
a=count;
}

int main()
{
string temp="444-43-2343-23432-33";
int aa[255];
int a=0;
cout< cat(temp,aa,a);
display(aa);
cout<<"共"< return 0;
}

回答(2):

#include
#include
#include
#include
using namespace std;

int main()
{
fstream a("1.txt");
string temp;
vector names;

while(a>>temp)
{
if(temp.find("name=\"") == -1)
continue;
size_t pos1=temp.find("\"");

// cout< if(temp.find("\"",pos1+1) == -1)
continue;
size_t pos2=temp.find("\"",pos1+1);

// cout< string ans(temp,pos1+1,pos2-pos1-1);

names.push_back(ans);
}

return 0;
}
!!!!!!!!!!!前提: 网页内容都放到1.txt中

都放到vector names;中了 程序比较粗糙,见谅

回答(3):

C++的准标准库Boost有正则表达式的类boost::regex
用那个可以很容易地把字符串匹配下来

当然,更标准的方法是解析XML,对规范的网页比较有用