本文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;
} }

C#字符串自增自减算法的更多相关文章

  1. java基础(二) 自增自减与贪心规则

    引言   JDK中提供了自增运算符++,自减运算符--.这两个操作符各有两种使用方式:前缀式(++ a,--a),后缀式(a++,a--).可能说到这里,说不得有读者就会吐槽说,前后缀式都挺简单的,前 ...

  2. 运算符:三目运算符,运算符优先级,sizeof,自增自减,取余

    一://---------运算符-----------// 1.运算符是告诉编译程序执行特定算术或逻辑操作的符号. 2.按照功能划分: 算术运算符. 关系运算符与逻辑运算符.按位运算符. 3.运算符根 ...

  3. day03运算符、表达式、自增自减、三目运算符、程序结构、用户输入

    复习 1.java的输出语句 1)System.out.println(); 2)System.out.print(); 2.注释 1)单行注释 // 2)多行注释 /* .... */ 3.变量 1 ...

  4. ipv4 ipv6 求字符串和整数一一映射的算法 AmazonOrderId

    字符串和整数一一映射的算法 公司每人的英文名不同,现在给每个英文名一个不同的数字编号,怎么设计? 走ipv4/6  2/32 2/128就够了,把“网段”概念对应到“表或库”,ip有a_e5类,这概念 ...

  5. 一文详解MySQL如何同时自增自减多个字段

    本文将带大家聊一下如何同时自增自减多个字段 开始之前,先分享一套MySQL教程,小白入门或者学习巩固都可以看 MySQL基础入门-mysql教程-数据库实战(MySQL基础+MySQL高级+MySQL ...

  6. 【转】 C语言自增自减运算符深入剖析

    转自:http://bbs.csdn.net/topics/330189207 C语言的自增++,自减--运算符对于初学者来说一直都是个难题,甚至很多老手也会产生困惑,最近我在网上看到一个问题:#in ...

  7. HDU3549 Flow Problem(网络流增广路算法)

    题目链接. 分析: 网络流增广路算法模板题.http://www.cnblogs.com/tanhehe/p/3234248.html AC代码: #include <iostream> ...

  8. C++ Primer 学习笔记_61_重载操作符与转换 --自增/自减操作符

    重载操作符与转换 --自增/自减操作符 引言: 自增,自减操作符常常由诸如迭代器这种类实现,这种类提供相似于指针的行为来訪问序列中的元素.比如,能够定义一个类,该类指向一个数组并为该数组中的元素提供訪 ...

  9. C: printf参数执行顺序与前置后置自增自减的影响

    起源: 今天在了解副作用side-effect的过程中,看到了下面的网页,把我带到了由printf引起的一系列问题,纠结了一整天,勉强弄懂. 第一个代码没什么好解释的.而第二个printf(" ...

随机推荐

  1. qtp重定义数组大小

    a dim arr1() ) a  dim arr() ReDim arr(a) arr arr ) arr For each i in arr     print arr(i) Next

  2. web安全之同源策略

    为什么使用同源策略?一个重要原因就是对cookie的保护,cookie 中存着sessionID .如果已经登录网站,同时又去了任意其他网站,该网站有恶意JS代码.如果没有同源策略,那么这个网站就能通 ...

  3. 10_android打包的过程

    java代码先编译成.class,最后打包成.dex.resources  uncompiled resources:不需要编译的资源:资产目录assets 清单文件 用来标识唯一的安卓应用:签名和包 ...

  4. geneid/genesymbol/ensemblid等之间的转换

    在基因注释时,难免碰到各种GENE在不同数据库之间的ID转换(例如,Ensembl ID 转Entrez ID,或者Entrez ID与GENE Symbol之间的转换).这里介绍一下常用的三个在线网 ...

  5. IDEA如何启动debug?

    选择remote,然后,修改host和port: host是我们访问backstop的ip,端口是监听端口8787,点击ok即可. 打断点,调试,即可.

  6. 【linux学习-centeros】

    1:linux的目录结构: / root —?启动Linux时使用的一些核心文件.如操作系统内核.引导程序Grub等. home —?存储普通用户的个人文件 ftp — 用户所有服务 httpd sa ...

  7. 【关于msyql5.6创建存储过程的一些记录】

    -- 秒杀执行存储过程DELETE $$ -- console的结束符号由;转换成 $$-- in输入参数:out:输出参数-- ROW_COUNT():返回上条dml影响的条数: 小于0:sql语句 ...

  8. 字符串中去除多余的空格保留一个(MS SQL Server)

    大约2年前,写过一篇<字符串中去除多余的空格保留一个(C#)>https://www.cnblogs.com/insus/p/7954151.html 今天,Insus.NET使用MS S ...

  9. 教大家一个看电视局免广告的方法--由UWP想到的

    将近一年(10个月)来一直在学习.NET技术,这其中包括C#.WPF.WCF和ASP.NET MVC,目前学习即将结束. 本人在学习WPF的过程中,也了解到有UWP这门技术,UWP技术目前来说主要是应 ...

  10. python之03编码学习

    编码介绍 ASCII :只能存英文和拉丁字符,一个字符占一个字节,8位 在中国的发展: gb2312:存6700多个中文      1980年 gbk1.0 :存2万多字符             1 ...