当前位置: 首页 > news >正文

C++ 字符串处理5-手机号邮箱如何脱敏处理

  • 1. 关键词
  • 2. strutil.h
  • 3. strutil.cpp
  • 4. 测试代码
  • 5. 运行结果
  • 6. 源码地址

1. 关键词

关键词:

C++ 字符串处理 分割字符串 连接字符串 跨平台

应用场景:

有些重要信息需要保密,比如手机号、邮箱等,如何在不影响用户阅读的情况下,将这些信息脱敏处理,以保障用户的隐私安全。

2. strutil.h

#pragma once#include <string>namespace cutl
{/*** @brief Desensitizing a string by replacing some characters with '*'.** @param str the string to be desensitized.* @return std::string the desensitized string.*/std::string desensitizing(const std::string &str);
} // namespace cutl

3. strutil.cpp

#include <cctype>
#include <algorithm>
#include "strutil.h"namespace cutl
{// 字符串脱敏处理std::string desensitizing(const std::string &str){std::string result;// 只打印前1/4和后1/4的内容,中间用*表示if (str.empty()){result = "";}else if (str.length() == 1){result = "*";}else if (str.length() == 2){result = str.substr(0, 1) + std::string(str.length() - 1, '*');}else if (str.length() <= 6){result = str.substr(0, 2) + std::string(str.length() - 2, '*');}else if (str.length() < 10){result = str.substr(0, 2) + std::string(str.length() - 4, '*') + str.substr(str.length() - 2, 2);}else if (str.length() < 16){// 长度控制在最长12位,中间×不超过6auto startCount = (str.length() - 6) > 6 ? 6 : (str.length() - 6);result = str.substr(0, 3) + std::string(startCount, '*') + str.substr(str.length() - 3, 3);}else{// 长度控制在最长12位result = str.substr(0, 4) + std::string(4, '*') + str.substr(str.length() - 4, 4);}return result;}
} // namespace cutl

4. 测试代码

#include "common.hpp"
#include "strutil.h"void TestDesensitizing()
{PrintSubTitle("desensitizing");std::string password = "2515774";std::cout << "password: " << cutl::desensitizing(password) << std::endl;std::string phone = "18500425678";std::cout << "phone: " << cutl::desensitizing(phone) << std::endl;
}

5. 运行结果

-------------------------------------------desensitizing--------------------------------------------
password: 25***74
phone: 185*****678

6. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

http://www.lryc.cn/news/373373.html

相关文章:

  • 【lesson8】云备份服务端完整版代码
  • AI办公自动化:kimi批量搜索提取PDF文档中特定文本内容
  • 基于C#开发web网页管理系统模板流程-总集篇
  • 什么是DMZ?路由器上如何使用DMZ?
  • 【bugfix】解决Redis缓存键清理问题
  • 泛微开发修炼之旅--15后端开发连接外部数据源,实现在ecology系统中查询其他异构系统数据库得示例和源码
  • 弹幕逆向signature、a_bogus
  • jEasyUI 使用标记创建树形菜单
  • IT人的拖延——拖是因为不想离开“舒适区”?
  • JUnit 5学习笔记
  • 西格玛 ------ 第18个希腊字母学习
  • 【C语言】assert.h——断言
  • HTML静态网页成品作业(HTML+CSS)—— 零食商城网页(1个页面)
  • 虚函数机制-动态绑定的应用
  • MOS开关电路应用于降低静态功耗
  • 【每日刷题】Day65
  • Oracle数据库连接并访问Microsoft SQL Server数据库
  • SQL 入门教程
  • Java—装饰器模式
  • 服务器远程桌面经常连接不上,造成远程桌面连接不上的原因都有哪些
  • C#|Maui|BootstrapBlazor|Bootstrap Blazor 组件库改模板 | Bootstrap Blazor 组件库改布局,该怎么改?
  • 【Linux】I/O多路复用
  • ubuntu20.0.4下安装PyTorch
  • Android屏幕旋转流程(1)
  • JS常见的运算符有哪些?
  • 【scikit-learn入门指南】:机器学习从零开始
  • MEMS:Lecture 17 Noise MDS
  • Windows运维:找到指定端口的服务
  • Linux文件系统讲解!
  • mysql集群,两主两从,使用mysql-proxy实现读写分离