Detailed introduction to C# string processing gadgets

黄舟
Release: 2017-03-17 11:43:32
Original
1503 people have browsed it

This article mainly introducesC#String processing gadgets, the functions include: convert to uppercase; convert to lowercase; reverse string; match the number of occurrences of a certain string; regular Match; base64 encryption; base64 decryption; ROT13 encryption and decryption; MD5 32-bit encryption. Has very good reference value. Let’s take a look with the editor below

I was obsessed with security when I first went to college. At that time, I always wanted to write a small program for processing strings.

Unfortunately, I didn’t have much time at the time, so I kept delaying it until this winter vacation.

I have nothing to do during the winter vacation, so I wrote a small program to practice my skills and review forms and basics.

The functions implemented are as follows:

Convert to uppercase

Convert to lowercase

Reverse the string

Match the number of occurrences of a certain string

Regular matching

base64 encryption

base64 decryption

ROT13 encryption and decryption

##MD5 32-bit encryption The program is still very simple, without robustness and no input verification.

Create BUGs with care (Be more careful

Also please don’t complain about my

variable

naming and method naming. If you haven’t learned Pinyin since elementary school, you will definitely not be able to understand it: )Because I started doing this in a blind testing project.

I’m really too lazy to translate it

There is a built-in method for converting to uppercase and lowercase

Console.WriteLine(s.ToUpper());//转换成大写
Console.WriteLine(s.ToLower());//转换成小写
Copy after login

Output reverse characters 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("反向字符串为{0}",s1);
  }
Copy after login

View the number of a certain short string in it

public static void pipei(string s)
  {
   int count = 0;
   int i;
   Console.WriteLine("请输入短字符串");
   string s2 = Console.ReadLine();
   while ((i=s.IndexOf(s2)) >= 0)
   {
    count++;
    s = s.Substring(i + s2.Length);
   }
   Console.WriteLine("字符串中出现了{0}次{1}", count, s2);
  }
Copy after login

Regular matching

Not learned I have learned a lot about regular classes, and I have read a lot online. Most of them talk about regular classes rather than regular classes. I was stuck writing this for about a day, and it still has bugs.

When there is no matching result, or the match is empty? Will cause multiple line breaks. I also forgot how I tested the bug at that time.

If any garden friends have any ideas, please share them.

public static void zzpipei(string s)
  {
   Console.WriteLine("请输入正则表达式");
   string zz = Console.ReadLine();
   Regex re = new Regex(zz);
   string s2 = "";
   if (re.IsMatch(s))
   {
    Console.WriteLine("匹配成功");
    MatchCollection mc = re.Matches(s);
    foreach (Match ma in mc)
    {
     s2 += ma.Value;
     s2 += ("\r\n");
    }
    Console.WriteLine("一行为一个匹配结果");
    Console.WriteLine(s2);
   }
   else
   { Console.WriteLine("无匹配结果"); }
  }
Copy after login

base64 encryption

The method used is also built-in. The encryption of Chinese characters is different from the encryption of some websites.

 public static void basejiami(string s)
  {
   byte[] bytes = Encoding.Default.GetBytes(s);
    Console.WriteLine("字符串base64加密为{0}", Convert.ToBase64String(bytes));
  }
Copy after login

base64 decryption

 public static void basejiemi(string s)
  {
   byte[] bytes = Convert.FromBase64String(s);
    Console.WriteLine("字符串base64解密为{0}", Encoding.Default.GetString(bytes));
  }
Copy after login

ROT13 encryption and decryption

ROT13 is a simple replacement password. ROT13 is also a variation of the Caesar cipher developed in the past in ancient Rome.

ROT13 is to replace 13 bits backward, that is, A is converted to N, B is converted to O, and so on.

The Caesar cipher replaces 3 digits backwards. This method can be modified to crack the Caesar cipher, and this method is case-sensitive.

ROT13 is its own inverse; that is, to restore ROT13, just apply the same encryption algorithm, so the same operation can be used for encryption and decryption.

This algorithm does not provide real cryptographic security, so it should not be used for purposes that require security. It is often cited as an example of weak encryption.

public static void rotjm(string s)
  {
   string jmzf = "";//解密加密后的字符串
   char[] arrey = s.ToCharArray();
   Console.WriteLine("字符串长度为{0}", arrey.Length);
   for (int i = 0; i < arrey.Length; i++)
   {
    int zfcode = (int)arrey[i];
    if (zfcode >= 97 && zfcode <= 109)
     zfcode = zfcode + 13;
    else if (zfcode >= 110 && zfcode <= 122)
     zfcode = zfcode - 13;
    else if (zfcode >= 65 && zfcode <= 77)
     zfcode = zfcode + 13;
    else if (zfcode >= 78 && zfcode <= 90)
     zfcode = zfcode - 13;
    jmzf = jmzf + (char)zfcode;
   }
   Console.WriteLine("结果为{0}", jmzf);
  }
Copy after login

Replace string

public static void thzf(string s)
  {
   Console.WriteLine("请输入想要被替换的字符串");
   string str1 = Console.ReadLine();
   Console.WriteLine("请输入想要替换成的字符串");
   string str2 = Console.ReadLine();
   Console.WriteLine(s.Replace(str1, str2));
  }
Copy after login

32-bit MD5 encryption

public static void md5jm(string s)
  {
   MD5 md5 = new MD5CryptoServiceProvider();
   //将字符编码为字节序列
   byte[] data = System.Text.Encoding.Default.GetBytes(s);
   byte[] md5data = md5.ComputeHash(data);
   md5.Clear();
   //遍历加密数组,加密字节,该方法为32位加密
   string str = "";
   for (int i = 0; i < md5data.Length; i++)
   {
    str += md5data[i].ToString("x").PadLeft(2, &#39;0&#39;);
   }
   Console.WriteLine("加密结果为{0}",str);
  }
Copy after login
My program, using .NET framework 4.0.

The above is the detailed content of Detailed introduction to C# string processing gadgets. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!