C#字符串自增自减算法
2024-08-26 02:22:32
本文URL:http://www.cnblogs.com/CUIT-DX037/p/6770535.html
实现字符串自增和自减运算:
1.数字从 0-9 变化;
2.字母从 A-Z、a-z 变化;
3.其它字符跳过;
4.以上变化依据其Ascii码值;
/// <summary>
/// 字符串运算
/// </summary>
public class StringOperation
{ /// <summary>
/// 通过ASCII码值,对字符串自增1
/// </summary>
/// <param name="pStr">输入字符串</param>
/// <returns></returns>
public static string StringIncreaseOne(string pStr)
{
var vRetStr = pStr;
if ( == pStr.Length)
{
vRetStr = "";
}
else
{
// 将最后一个字符与之前的字符串分开
string vOtherStr = pStr.Substring(, pStr.Length - );
int vIntChar = (int)pStr[pStr.Length - ]; //转ASCII码值
if ( <= vIntChar && vIntChar <= ) //是数字(0 - 9)
{
vIntChar++; //自增1
if (vIntChar == ) // 进一位
{
vIntChar = ;
vOtherStr = StringIncreaseOne(vOtherStr);
}
}
else if ( <= vIntChar && vIntChar <= ) //是字母(A - Z)
{
vIntChar++; //自增1
if (vIntChar == )
{
vIntChar = ;
vOtherStr = StringIncreaseOne(vOtherStr);
}
}
else if ( <= vIntChar && vIntChar <= ) //是字母(a - z)
{
vIntChar++; //自增1
if (vIntChar == )
{
vIntChar = ;
vOtherStr = StringIncreaseOne(vOtherStr);
}
}
else // 其它字符 -> 跳过
{
vOtherStr = StringIncreaseOne(vOtherStr);
}
vRetStr = vOtherStr + (char)vIntChar;
}
return vRetStr;
} /// <summary>
/// 通过ASCII码值,对字符串自减1
/// </summary>
/// <param name="pStr">输入字符串</param>
/// <returns></returns>
public static string StringReducingOne(string pStr)
{
var vRetStr = pStr;
if ( == pStr.Length)
{
vRetStr = "";
}
else
{
string vOtherStr = pStr.Substring(, pStr.Length - );
int vIntChar = (int)pStr[pStr.Length - ]; //转ASCII码值
if ( <= vIntChar && vIntChar <= ) //是数字(0 - 9)
{
vIntChar--;
if (vIntChar == )
{
vIntChar = ;
vOtherStr = StringReducingOne(vOtherStr);
}
}
else if ( <= vIntChar && vIntChar <= ) //是数字(A - Z)
{
vIntChar--;
if (vIntChar == )
{
vIntChar = ;
vOtherStr = StringReducingOne(vOtherStr);
}
}
else if ( <= vIntChar && vIntChar <= ) //是数字(a - z)
{
vIntChar--;
if (vIntChar == )
{
vIntChar = ;
vOtherStr = StringReducingOne(vOtherStr);
}
}
else // 其它字符 -> 跳过
{
vOtherStr = StringReducingOne(vOtherStr);
}
vRetStr = vOtherStr + (char)vIntChar;
}
return vRetStr;
} /// <summary>
/// 通过ASCII码值,对字符串自增
/// </summary>
/// <param name="pStr">输入字符串</param>
/// <param name="pCount">自增个数</param>
/// <returns></returns>
public static string StringIncrease(string pStr, int pCount)
{
string vRetStr = pStr;
for (int i = ; i < pCount; i++)
{
vRetStr = StringIncreaseOne(vRetStr);
}
return vRetStr;
} /// <summary>
/// 通过ASCII码值,对字符串自减
/// </summary>
/// <param name="pStr">输入字符串</param>
/// <param name="pCount">自减个数</param>
/// <returns></returns>
public static string StringReducing(string pStr, int pCount)
{
string vRetStr = pStr;
for (int i = ; i < pCount; i++)
{
vRetStr = StringReducingOne(vRetStr);
}
return vRetStr;
} }
最新文章
- Oracle 用户、角色、权限(系统权限、对象权限)的数据字典表
- 由于客户端检测到一个协议错误 代码0x1104
- Java核心技术点之内部类
- 【leetcode】Remove Linked List Elements(easy)
- 关于为busybox设置setuid
- redhat6.3安装MySQL-server-5.6.13-1.el6.x86_64.rpm
- 关于MediaPlayer的详细介绍
- Couchbase之个人描述及入门示例
- windos多线程编程
- Spark 资源调度及任务调度
- php设计模式笔记--总结篇
- linux shell命令之 xargs
- Kali Linux NetHunter教程Kali NetHunter支持的设备和ROMs
- [转]PHP时区/MySql时区/Linux时区
- 好系统重装助手教你如何让win10系统快速开机
- PC机Win10声音问题两例处理办法
- str_replace使用
- boost 时间
- [真题] 一道 vsftp 运维题
- Mac 10.12安装截图工具Jietu
热门文章
- [原创] 新人分享--ORA-01012:not logged on的解决办法 [复制链接]
- javaScript之深度理解原型链
- Socket对象以及异常
- Fast Walsh–Hadamard transform
- 6、git和github
- ARC097D Equals
- Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute
- linux下sed批量替换文件内容
- How to install Samba server on Ubuntu 12.04
- dos下操作Mysql数据库