• C#字符串自增自减算法


    本文URL:http://www.cnblogs.com/CUIT-DX037/p/6770535.html 

    实现字符串自增和自减运算:

    1.数字从 0-9 变化;

    2.字母从 A-Z、a-z 变化;

    3.其它字符跳过;

    4.以上变化依据其Ascii码值;

      1     /// <summary>
      2     /// 字符串运算
      3     /// </summary>
      4     public class StringOperation
      5     {
      6 
      7 
      8         /// <summary>
      9         /// 通过ASCII码值,对字符串自增1
     10         /// </summary>
     11         /// <param name="pStr">输入字符串</param>
     12         /// <returns></returns>
     13         public static string StringIncreaseOne(string pStr)
     14         {
     15             var vRetStr = pStr;
     16             if (0 == pStr.Length)
     17             {
     18                 vRetStr = "1";
     19             }
     20             else
     21             {
     22                 // 将最后一个字符与之前的字符串分开
     23                 string vOtherStr = pStr.Substring(0, pStr.Length - 1);
     24                 int vIntChar = (int)pStr[pStr.Length - 1]; //转ASCII码值
     25                 if (48 <= vIntChar && vIntChar <= 57) //是数字(0 - 9)
     26                 {
     27                     vIntChar++; //自增1
     28                     if (vIntChar == 58) // 进一位
     29                     {
     30                         vIntChar = 48;
     31                         vOtherStr = StringIncreaseOne(vOtherStr);
     32                     }
     33                 }
     34                 else if (65 <= vIntChar && vIntChar <= 90) //是字母(A - Z)
     35                 {
     36                     vIntChar++; //自增1
     37                     if (vIntChar == 91) 
     38                     {
     39                         vIntChar = 65;
     40                         vOtherStr = StringIncreaseOne(vOtherStr);
     41                     }
     42                 }
     43                 else if (97 <= vIntChar && vIntChar <= 122) //是字母(a - z)
     44                 {
     45                     vIntChar++; //自增1
     46                     if (vIntChar == 123)
     47                     {
     48                         vIntChar = 97;
     49                         vOtherStr = StringIncreaseOne(vOtherStr); 
     50                     }
     51                 }
     52                 else // 其它字符 -> 跳过
     53                 {
     54                     vOtherStr = StringIncreaseOne(vOtherStr);
     55                 }
     56                 vRetStr = vOtherStr + (char)vIntChar;
     57             }
     58             return vRetStr;
     59         }
     60 
     61         /// <summary>
     62         /// 通过ASCII码值,对字符串自减1
     63         /// </summary>
     64         /// <param name="pStr">输入字符串</param>
     65         /// <returns></returns>
     66         public static string StringReducingOne(string pStr)
     67         {
     68             var vRetStr = pStr;
     69             if (0 == pStr.Length)
     70             {
     71                 vRetStr = "9";
     72             }
     73             else
     74             {
     75                 string vOtherStr = pStr.Substring(0, pStr.Length - 1);
     76                 int vIntChar = (int)pStr[pStr.Length - 1]; //转ASCII码值
     77                 if (48 <= vIntChar && vIntChar <= 57) //是数字(0 - 9)
     78                 {
     79                     vIntChar--;
     80                     if (vIntChar == 47)
     81                     {
     82                         vIntChar = 57;
     83                         vOtherStr = StringReducingOne(vOtherStr);
     84                     }
     85                 }
     86                 else if (65 <= vIntChar && vIntChar <= 90) //是数字(A - Z)
     87                 {
     88                     vIntChar--; 
     89                     if (vIntChar == 64)
     90                     {
     91                         vIntChar = 90;
     92                         vOtherStr = StringReducingOne(vOtherStr);
     93                     }
     94                 }
     95                 else if (97 <= vIntChar && vIntChar <= 122) //是数字(a - z)
     96                 {
     97                     vIntChar--;
     98                     if (vIntChar == 96)
     99                     {
    100                         vIntChar = 122;
    101                         vOtherStr = StringReducingOne(vOtherStr);
    102                     }
    103                 }
    104                 else // 其它字符 -> 跳过
    105                 {
    106                     vOtherStr = StringReducingOne(vOtherStr);
    107                 }
    108                 vRetStr = vOtherStr + (char)vIntChar;
    109             }
    110             return vRetStr;
    111         }
    112 
    113         /// <summary>
    114         /// 通过ASCII码值,对字符串自增
    115         /// </summary>
    116         /// <param name="pStr">输入字符串</param>
    117         /// <param name="pCount">自增个数</param>
    118         /// <returns></returns>
    119         public static string StringIncrease(string pStr, int pCount)
    120         {
    121             string vRetStr = pStr;
    122             for (int i = 0; i < pCount; i++)
    123             {
    124                 vRetStr = StringIncreaseOne(vRetStr);
    125             }
    126             return vRetStr;
    127         }
    128 
    129         /// <summary>
    130         /// 通过ASCII码值,对字符串自减
    131         /// </summary>
    132         /// <param name="pStr">输入字符串</param>
    133         /// <param name="pCount">自减个数</param>
    134         /// <returns></returns>
    135         public static string StringReducing(string pStr, int pCount)
    136         {
    137             string vRetStr = pStr;
    138             for (int i = 0; i < pCount; i++)
    139             {
    140                 vRetStr = StringReducingOne(vRetStr);
    141             }            
    142             return vRetStr;
    143         }
    144         
    145 
    146 
    147     }
  • 相关阅读:
    组件GIS 0 前言
    GIS数据结构与算法
    GIS数据结构与算法 0 前言
    Git推送本地工程到远程仓库
    为知笔记+Typora+PicGo发表博客园博客
    时间记录"时间块"的使用技巧
    WebGIS学习路线
    [c++指针教程]用简单链表练习指针
    动态规划题目整理
    图论刷题整理
  • 原文地址:https://www.cnblogs.com/CUIT-DX037/p/6770535.html
Copyright © 2020-2023  润新知