题解 | #截取字符串#
截取字符串
https://www.nowcoder.com/practice/a30bbc1a0aca4c27b86dd88868de4a4a
#include <iostream>
#include <string>    // 仅包含需要的头文件
#include <stdexcept> // 包含标准异常处理
using namespace std;
int main() {
    string inputString; // 更具描述性的变量名
    int substringLength;
    cin >> inputString; // 读取字符串
    cin >> substringLength; // 读取整数
    try {
        // 检查substringLength是否大于字符串长度
        if (substringLength > inputString.length()) {
            throw out_of_range("The specified length exceeds the string length.");
        }
        cout << inputString.substr(0, substringLength) << endl; // 输出子字符串
    } catch (const out_of_range& e) {
        cerr << "Error: " << e.what() << endl; // 错误处理
    }
    return 0;
}
 投递用友等公司10个岗位
投递用友等公司10个岗位