C/C++ 关于String的相关使用
#include <iostream>
#include <string>
using namespace std;
#include<vector>
int main(int argc, char* argv[])
{
std::vector <int> a;
int intCount = 0;
unsigned int loc = 0;
string s = "zzzz xxcacv ffffff aaa";
while ((loc = s.find(" ", loc)) != string::npos)//查找字符串中空格
{
a.push_back(loc);
cout << loc++ << endl;//loc++;一定要有这句话,不然会死循环
}
if (a.size() != 0)
{
if (s.length() > 7)//判断字符串的长度是否大于7
{
for (int i = 0; i < a.size(); i++)
{
if (a[i] > 7)
{
s = s.replace(a[i - 1], 1, "\n");//将字符串中某个位置起的空格,长度为1,用“\n”替换
break;
}
}
int b = a.size() - 1;
if (s.length() - a[b] > 25)
{
s = s.replace(a[b], 1, "\n");
}
}
}
//直接输出即可
cout << s<< endl;
cin.get();
return 0;
#include <string>
using namespace std;
#include<vector>
int main(int argc, char* argv[])
{
std::vector <int> a;
int intCount = 0;
unsigned int loc = 0;
string s = "zzzz xxcacv ffffff aaa";
while ((loc = s.find(" ", loc)) != string::npos)//查找字符串中空格
{
a.push_back(loc);
cout << loc++ << endl;//loc++;一定要有这句话,不然会死循环
}
if (a.size() != 0)
{
if (s.length() > 7)//判断字符串的长度是否大于7
{
for (int i = 0; i < a.size(); i++)
{
if (a[i] > 7)
{
s = s.replace(a[i - 1], 1, "\n");//将字符串中某个位置起的空格,长度为1,用“\n”替换
break;
}
}
int b = a.size() - 1;
if (s.length() - a[b] > 25)
{
s = s.replace(a[b], 1, "\n");
}
}
}
//直接输出即可
cout << s<< endl;
cin.get();
return 0;
}
eg:
string name="ztztztzt";
name = name.insert(4, "\n");