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
variablenaming 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 lowercaseConsole.WriteLine(s.ToUpper());//转换成大写
Console.WriteLine(s.ToLower());//转换成小写
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);
}
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);
}
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("无匹配结果"); } }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)); }base64 decryption
public static void basejiemi(string s)
{
byte[] bytes = Convert.FromBase64String(s);
Console.WriteLine("字符串base64解密为{0}", Encoding.Default.GetString(bytes));
}
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); }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));
}
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, '0');
}
Console.WriteLine("加密结果为{0}",str);
}
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!

C# and .NET provide powerful features and an efficient development environment. 1) C# is a modern, object-oriented programming language that combines the power of C and the simplicity of Java. 2) The .NET framework is a platform for building and running applications, supporting multiple programming languages. 3) Classes and objects in C# are the core of object-oriented programming. Classes define data and behaviors, and objects are instances of classes. 4) The garbage collection mechanism of .NET automatically manages memory to simplify the work of developers. 5) C# and .NET provide powerful file operation functions, supporting synchronous and asynchronous programming. 6) Common errors can be solved through debugger, logging and exception handling. 7) Performance optimization and best practices include using StringBuild

.NETFramework is a cross-language, cross-platform development platform that provides a consistent programming model and a powerful runtime environment. 1) It consists of CLR and FCL, which manages memory and threads, and FCL provides pre-built functions. 2) Examples of usage include reading files and LINQ queries. 3) Common errors involve unhandled exceptions and memory leaks, and need to be resolved using debugging tools. 4) Performance optimization can be achieved through asynchronous programming and caching, and maintaining code readability and maintainability is the key.

Reasons for C#.NET to remain lasting attractive include its excellent performance, rich ecosystem, strong community support and cross-platform development capabilities. 1) Excellent performance and is suitable for enterprise-level application and game development; 2) The .NET framework provides a wide range of class libraries and tools to support a variety of development fields; 3) It has an active developer community and rich learning resources; 4) .NETCore realizes cross-platform development and expands application scenarios.

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
