c++ string 常用封装

bool replace(std::string& str, const std::string& from, const std::string& to) {
    size_t start_pos = str.find(from);
    if(start_pos == std::string::npos)
        return false;
    str.replace(start_pos, from.length(), to);
    return true;
}

std::string Lcase(std::string& str)
{
    int i;
    std::string temp = "";
    for(i = 0; i < str.length(); i++){
        temp += tolower(str[i]);
    }
    return temp;
}

std::string Ucase(std::string& str)
{
    int i;
    std::string temp = "";
    for(i = 0; i < str.length(); i++){
        temp += toupper(str[i]);
    }
    return temp;
}

std::string Mid(std::string& str, int pos1, int pos2)
{
    int i;
    std::string temp = "";
    for(i = pos1; i < pos2; i++){
        temp += str[i];
    }

    return temp;
}

std::string Left(std::string& str, int pos)
{
    int i;
    std::string temp = "";
    for(i = 0; i < pos; i++){
        temp += str[i];
    }

    return temp;
}

std::string Right(std::string& str, int pos)
{
    int i;
    std::string temp = "";
    for(i = pos; i < strlen(str.c_str()); i++)
    {
        temp += str[i];
    }
    return temp;
}

 

你可能感兴趣的:(c++)