您好,登錄后才能下訂單哦!
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; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。