当前位置: 首页 > 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。

相关文章:

  • How To: Localize Bar and Ribbon Skin Items
  • 【超详细】使用RedissonClient实现Redis分布式锁
  • ArrayList集合+综合案例
  • 英语复习之英语同音词总结(六)
  • MySQL 中 Varchar(50) 和 varchar(500) 区别是什么?
  • React常见面试题(2024最新版)
  • Postman简介
  • 鸿蒙低代码开发的局限性
  • STM32项目分享:智能窗帘系统
  • 设置服务器禁止和ip通信
  • C语言 | Leetcode C语言题解之第145题二叉树的后序遍历
  • MySQL Online DDL原理解读
  • Scala学习笔记12: 高阶函数
  • Qwen-Agent:Qwen2加持,强大的多代理框架 - 函数调用、代码解释器以及 RAG!
  • Hbase搭建教程
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • Computed property XXX was assigned to but it has no setter
  • CSS 三角实现
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  • Hibernate【inverse和cascade属性】知识要点
  • MD5加密原理解析及OC版原理实现
  • npx命令介绍
  • quasar-framework cnodejs社区
  • Redux 中间件分析
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • vue.js框架原理浅析
  • Vue实战(四)登录/注册页的实现
  • 蓝海存储开关机注意事项总结
  • 前端路由实现-history
  • 前端面试总结(at, md)
  • 思维导图—你不知道的JavaScript中卷
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 想使用 MongoDB ,你应该了解这8个方面!
  • mysql面试题分组并合并列
  • 数据可视化之下发图实践
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • #中的引用型是什么意识_Java中四种引用有什么区别以及应用场景
  • (11)MSP430F5529 定时器B
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (java)关于Thread的挂起和恢复
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (第30天)二叉树阶段总结
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (十五)使用Nexus创建Maven私服
  • (一)Neo4j下载安装以及初次使用
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (原創) 如何優化ThinkPad X61開機速度? (NB) (ThinkPad) (X61) (OS) (Windows)
  • .md即markdown文件的基本常用编写语法
  • .NET Compact Framework 多线程环境下的UI异步刷新