Home Backend Development C#.Net Tutorial Detailed introduction to C# string processing gadgets

Detailed introduction to C# string processing gadgets

Mar 17, 2017 am 11:43 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

How to change the format of xml How to change the format of xml Apr 03, 2025 am 08:42 AM

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages ​​such as Python. Be careful when modifying and back up the original files.

See all articles