This article mainly introduces the C # string processing gadget, which includes: converting to uppercase, converting to lowercase, reversing string, matching string occurrences, regular matching, base64 encryption, Base64 decryption, ROT13 encryption and decryption, MD5 32-bit encryption. Has a good reference value. Let's take a look at the little series.
When I first went to college, I was obsessed with security, and was trying to write a small program that dealt with strings.
Unfortunately, there was not much time, has been delayed to this winter vacation.
Winter vacation is idle, so write a small program to practiced hand, by the way to review the form and the foundation.
The following functions are implemented:
Convert to uppercase
Convert to lowercase
Invert string
Matches the number of occurrences of a string
Regular match
Base64 encryption
Base64 decryption
ROT13 Encryption and decryption
MD5 32-bit encryption
The program is still very primitive, there is no robustness, and there is no input verification.
Create Bugs with your heart
And please do not vomit groove my variable name and method naming, if you are not starting from primary school to learn pinyin must not understand:)
Because it was done in a blind test project at the beginning.
I'm too lazy to translate.
Conversion to uppercase and lowercase is a self-bringing method.
Console.WriteLine (S.toupper ());//Convert to uppercase Console.WriteLine (S.tolower ());//change to lowercase
Output reverse String
public static void Fanxiang (string s) { char[] Arrey = S.tochararray (); StringBuilder S1 = new StringBuilder (""); for (int i = Arrey. Length-1; I >= 0; i--) { S1. Append (Convert.ToString (arrey[i)); } Console.WriteLine ("Inverse string = {0}", S1); }
View the number of a short string in it
public static void Pipei (string s) { int count = 0; int i; Console.WriteLine ("Please enter a short string"); String s2 = Console.ReadLine (); while ((I=s.indexof (S2)) >= 0) { count++; s = s.substring (i + s2. Length); } Console.WriteLine ("{0} times {1}" appears in the string, count, S2); }
Regular match
Without learning the knowledge of regular classes, many of the online reading is about regular rather than regular classes. At that time wrote this probably card one day, now this still has the bug.
No matching results, or matching to empty? Causes multiple lines to be wrapped. I also forgot how the bug was tested.
Which garden Friend has the idea to say.
public static void Zzpipei (string s) { Console.WriteLine ("Please enter regular expression"); String zz = Console.ReadLine (); Regex re = new regex (ZZ); String s2 = ""; if (re. IsMatch (s)) { Console.WriteLine ("Match succeeded"); MatchCollection mc = Re. Matches (s); foreach (Match MA in mc) { s2 + = ma. Value; S2 + = ("\ r \ n"); } Console.WriteLine ("One row is a matching result"); Console.WriteLine (s2); } Else {Console.WriteLine ("No match result");} }
Base64 encryption
The method used is also self-bringing, the encryption of Chinese characters and some Web site encryption is not the same.
public static void Basejiami (string s) { byte[] bytes = Encoding.Default.GetBytes (s); Console.WriteLine ("String base64 encryption is {0}", convert.tobase64string (bytes)); }
Base64 decryption
public static void Basejiemi (string s) { byte[] bytes = convert.frombase64string (s); Console.WriteLine ("String base64 decrypted to {0}", Encoding.Default.GetString (bytes)); }
ROT13 Encryption and decryption
ROT13 is a simple replacement cipher. ROT13 is also a variant of Caesar's encryption, which was developed in ancient Rome.
ROT13 is to replace 13 bits backwards, i.e. a to N,b to O and so on.
The Caesar password is replaced 3 bits backwards. This method can also be changed to realize the explosion of Caesar's password, and the method is case-sensitive.
ROT13 is its own reverse, that is, to restore the ROT13, the same algorithm can be used to encrypt, so the same operation can be re-encrypted and decrypted.
The algorithm does not provide true cryptographic security, so it should not be applied to the purpose of preservation. It is often used as a typical example of weak cryptography.
public static void Rotjm (string s) { string jmzf = "";//Decrypt the encrypted string char[] Arrey = S.tochararray (); Console.WriteLine ("String length is {0}", Arrey. Length); for (int i = 0; i < Arrey. Length; i++) { int zfcode = (int) arrey[i]; if (Zfcode >= && zfcode <= 109) Zfcode = Zfcode +; else if (zfcode >= && zfcode <= 122) zfcode = zfcode-13; else if (zfcode >= && zfcode <=) zfcode = Zfcode +; else if (zfcode >= && zfcode <=) zfcode = zfcode-13; JMZF = Jmzf + (char) zfcode; } Console.WriteLine ("Result {0}", JMZF); }
Replace string
public static void Thzf (string s) { Console.WriteLine ("Please enter a string to be replaced"); String str1 = Console.ReadLine (); Console.WriteLine ("Please enter the string you want to replace"); String str2 = Console.ReadLine (); Console.WriteLine (S.replace (STR1, str2)); }
32-bit MD5 encryption
public static void Md5jm (string s) { MD5 md5 = new MD5CryptoServiceProvider (); Encodes a character into a byte sequence byte[] data = System.Text.Encoding.Default.GetBytes (s); byte[] Md5data = md5.computehash (data); MD5. Clear (); Iterate through the encrypted array, encrypt the bytes, the method is 32-bit encrypted string str = ""; for (int i = 0; i < Md5data. Length; i++) { str + = Md5data[i]. ToString ("X"). PadLeft (2, ' 0 '); } Console.WriteLine ("Encrypted result is {0}", str); }
My program, using the. NET Framework 4.0.