zoukankan      html  css  js  c++  java
  • 字符串操作>字符串编码和使用正则表达式

    字符串操作>字符串编码

    System.Text提供了Encoding的抽象类,这个类提供字符串编码的方法。使Unicode字符数组的字符串,转换为指定编码的字节数组,或者反之。

    Unicode有四种编码格式,UTF-8, UTF-16,UTF-32,UTF-7。

    字符编码类,ASCIIEncoding ,UTF7Encoding,UnicodeEncoding,UTF32Encoding。

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace AsciiEncodingDemo
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                ASCIIEncoding myAscii 
    = new ASCIIEncoding();
                
    string unicodeStr = "ASCII Encoding Demo";
                Console.WriteLine(unicodeStr);
                
    //下面的代码将对unicodeStr字符串的内容进行编码。
                Byte[] encodeBytes = myAscii.GetBytes(unicodeStr);
                Console.WriteLine(
    "编码后的字符串:");
                
    foreach (byte c in encodeBytes)
                {
                    Console.Write(
    "[{0}]", c);
                }
                Console.WriteLine(
    "");
                Console.WriteLine(
    "解码后的字符串:");
                
    //下面的语句将对encodeBytes字节数组的内容进行解码
                string decodeStr = myAscii.GetString(encodeBytes);
                Console.WriteLine(decodeStr);
                Console.ReadLine();         
             }
        }
    }


    字符串操作>使用正则表达式

    在System.Text.RegularExpression命名空间里,有正则表达式方法。

    例子1
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;

    namespace RegexDemo
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Regex regex 
    = new Regex("China", RegexOptions.IgnoreCase);
                
    //使用Match方法。
                string source = "China is my mother,My mother is china!";
                Match m 
    = regex.Match(source);
                
    if (m.Success)
                {
                    Console.WriteLine(
    "找到第一个匹配");
                }
                Console.WriteLine(
    new string('-',9));
                
    //下面的样例将演示使用Matches方法进行匹配
                MatchCollection matches=regex.Matches(source);
                
    foreach(Match s in matches)
                {
                    
    if(s.Success)
                        Console.WriteLine(
    "找到了一个匹配");
                }
                Console.ReadLine();
             }
        }
    }


    例子2
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;

    namespace URLRegex
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    string Pattern = @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&$%\$#\=~])*$";
                Regex r 
    = new Regex(Pattern);
                
    string source = "http://www.163.com";
                Match m 
    = r.Match(source);
                
    if (m.Success)
                {
                    Console.WriteLine(
    "URL验证成功!");
                }
                
    else
                {
                    Console.WriteLine(
    "URL验证失败!");
                }
                Console.ReadLine();
            }
        }
    }



    合乎自然而生生不息。。。
  • 相关阅读:
    最大子数组的求解(包括首尾相接成环)
    学习进度条第五周
    学习进度条第四周
    学习进度条第三周
    软件工程概论作业二
    软件工程个人作业01
    软件工程概论总结
    梦断代码阅读笔记01
    构建之法阅读笔记04
    构建之法阅读笔记03
  • 原文地址:https://www.cnblogs.com/samwu/p/1847941.html
Copyright © 2011-2022 走看看