欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

C#字符串自增自减算法详解

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

C#实现字符串自增和自减运算,供大家参考,具体内容如下

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 (0 == pStr.Length)
   {
    vRetStr = "1";
   }
   else
   {
    // 将最后一个字符与之前的字符串分开
    string vOtherStr = pStr.Substring(0, pStr.Length - 1);
    int vIntChar = (int)pStr[pStr.Length - 1]; //转ASCII码值
    if (48 <= vIntChar && vIntChar <= 57) //是数字(0 - 9)
    {
     vIntChar++; //自增1
     if (vIntChar == 58) // 进一位
     {
      vIntChar = 48;
      vOtherStr = StringIncreaseOne(vOtherStr);
     }
    }
    else if (65 <= vIntChar && vIntChar <= 90) //是字母(A - Z)
    {
     vIntChar++; //自增1
     if (vIntChar == 91) 
     {
      vIntChar = 65;
      vOtherStr = StringIncreaseOne(vOtherStr);
     }
    }
    else if (97 <= vIntChar && vIntChar <= 122) //是字母(a - z)
    {
     vIntChar++; //自增1
     if (vIntChar == 123)
     {
      vIntChar = 97;
      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 (0 == pStr.Length)
   {
    vRetStr = "9";
   }
   else
   {
    string vOtherStr = pStr.Substring(0, pStr.Length - 1);
    int vIntChar = (int)pStr[pStr.Length - 1]; //转ASCII码值
    if (48 <= vIntChar && vIntChar <= 57) //是数字(0 - 9)
    {
     vIntChar--;
     if (vIntChar == 47)
     {
      vIntChar = 57;
      vOtherStr = StringReducingOne(vOtherStr);
     }
    }
    else if (65 <= vIntChar && vIntChar <= 90) //是数字(A - Z)
    {
     vIntChar--; 
     if (vIntChar == 64)
     {
      vIntChar = 90;
      vOtherStr = StringReducingOne(vOtherStr);
     }
    }
    else if (97 <= vIntChar && vIntChar <= 122) //是数字(a - z)
    {
     vIntChar--;
     if (vIntChar == 96)
     {
      vIntChar = 122;
      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 = 0; 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 = 0; i < pCount; i++)
   {
    vRetStr = StringReducingOne(vRetStr);
   }   
   return vRetStr;
  }
  


 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

上一篇:C# 屏蔽由于崩溃弹出的windows异常弹框

栏    目:C#教程

下一篇:C#程序员应该养成的程序性能优化写法

本文标题:C#字符串自增自减算法详解

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5547.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有