C++中string库对字符串的URL编码/解码

发布时间:2024-10-09 18:37:16 作者:小樊
来源:亿速云 阅读:78

在 C++ 中,可以使用标准库中的 <string><codecvt> 头文件来实现字符串的 URL 编码和解码。具体实现方式如下:

URL 编码

可以使用 std::url_encode 函数将字符串转换为 URL 编码格式。该函数接受两个参数,第一个参数为需要编码的字符串,第二个参数为编码后的字符串。函数返回值为编码后的字符串。

示例代码:

#include <iostream>
#include <string>
#include <codecvt>
#include <url>

int main() {
    std::string str = "Hello, World!";
    std::string encoded_str = std::url_encode(str);
    std::cout << "Encoded string: " << encoded_str << std::endl;
    return 0;
}

输出结果:

Encoded string: Hello%2C+World%21

URL 解码

可以使用 std::url_decode 函数将 URL 编码格式的字符串转换为正常字符串。该函数接受两个参数,第一个参数为需要解码的字符串,第二个参数为解码后的字符串。函数返回值为解码后的字符串。

示例代码:

#include <iostream>
#include <string>
#include <codecvt>
#include <url>

int main() {
    std::string encoded_str = "Hello%2C+World%21";
    std::string decoded_str = std::url_decode(encoded_str);
    std::cout << "Decoded string: " << decoded_str << std::endl;
    return 0;
}

输出结果:

Decoded string: Hello, World!
推荐阅读:
  1. c++右值引用和移动构造是什么
  2. c++右值引用和移动构造实例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:string库与C++标准库容器的交互

下一篇:string库在数据库操作中的应用场景

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》