C#字符串处理小工具的详细介绍

黄舟
Libérer: 2017-03-17 11:43:32
original
1463 Les gens l'ont consulté

本文主要介绍C#字符串处理小工具,实现功能包括:转换为大写;转换为小写;反转字符串;匹配某字符串出现次数;正则匹配;base64加密;base64解密;ROT13加密解密;MD5 32位加密。具有很好的参考价值。下面跟着小编一起来看下吧

之前刚上大学时沉迷于安全方面,当时一直想写一个处理字符串的小程序。

无奈当时没有太多时间,一直拖延到这寒假。

寒假闲来无事,所以就写写小程序来练手,顺便复习一下窗体和基础。

实现的功能有以下:

转换为大写

转换为小写

反转字符串

匹配某字符串出现次数

正则匹配

base64加密

base64解密

ROT13加密解密

MD5 32位加密

程序还是非常简陋的,没有健壮性,也没有输入的校验。

用心创造BUG(比心

还有请不要吐槽我的变量命名以及方法命名,如果你不是从小学开始学拼音肯定看不懂:)

因为一开始做这个是在瞎测试的项目里做起来的。

实在是懒得去翻译了

转换为大写和小写是有自带的方法的

Console.WriteLine(s.ToUpper());//转换成大写
Console.WriteLine(s.ToLower());//转换成小写
Copier après la connexion

输出反向字符串

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);
  }
Copier après la connexion

查看某一短字符串在其中的数量

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);
  }
Copier après la connexion

正则匹配

没有学过正则类的知识,网上看了很多大部分都是讲正则而不是正则类的。当时写这个大概卡了一天,现在这个依然有BUG。

没有匹配结果时,或者匹配到空?会造成多行换行。我也忘了当时是怎么测试出来的那个BUG。

哪位园友有想法可以说一下。

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("无匹配结果"); }
  }
Copier après la connexion

base64加密

使用的方法也是自带的,对于汉字的加密和部分网站的加密不一样。

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

base64解密

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

ROT13加密解密

ROT13是一种简易的置换暗码。ROT13 也是过去在古罗马开发的凯撒加密的一种变体。

ROT13是向后替换13位,即A转为N,B转为O以此类推。

凯撒密码是向后替换3位。这个方法再改一下还可以实现凯撒密码的爆破,而且该方法是区分大小写的。

ROT13是它自己本身的逆反;也就是说,要还原ROT13,套用加密同样的算法即可得,故同样的操作可用再加密与解密。

该算法并没有提供真正的密码学上的保全,故它不应该套用在需要保全的用途上。它常常被当作弱加密示例的典型。

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);
  }
Copier après la connexion

替换字符串

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

32位MD5加密

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);
  }
Copier après la connexion

我的程序,使用.NET framework 4.0。

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!