题解 | #截取字符串#
截取字符串
https://www.nowcoder.com/practice/a30bbc1a0aca4c27b86dd88868de4a4a
///////////////////普通for循环通过索引读取/////////////////
// #include <iostream>
// #include<string>
// using namespace std;
// int main() {
// string a;
// string strOut;
// int k;
// //a和k以空白符换行符作为停止读入的分隔线
// while (cin >> a >> k) {
// for (int i = 0; i < k; i++) {
// strOut += a[i];//索引访问字符串数组
// }
// cout << strOut << endl;
// strOut.clear();//清空字符串
// }
// return 0;
// }
//////////////////通过string的substr函数进行字符串截取////////////////////
#include <iostream>
#include<string>
using namespace std;
int main() {
string s;
int k;
while (cin >> s >> k)
{
//substr( int pos位置,int 个数n)
cout << s.substr(0, k) << endl;
}
return 0;
}
