• C#知识点<4>


    1C# 运算符重载

    您可以重定义或重载 C# 中内置的运算符。因此,程序员也可以使用用户自定义类型的运算符。重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的。与其他函数一样,重载运算符有返回类型和参数列表。

    例如,请看下面的函数:

    publicstaticBoxoperator+(Box b,Box c){Box box =newBox();
       box.length = b.length + c.length;
       box.breadth = b.breadth + c.breadth;
       box.height = b.height + c.height;return box;}

    上面的函数为用户自定义的类 Box 实现了加法运算符(+)。它把两个 Box 对象的属性相加,并返回相加后的 Box 对象。

    运算符重载的实现

    下面的程序演示了完整的实现:

    usingSystem;namespaceOperatorOvlApplication{classBox{privatedouble length;// 长度privatedouble breadth;// 宽度privatedouble height;// 高度publicdouble getVolume(){return length * breadth * height;}publicvoid setLength(double len ){
             length = len;}publicvoid setBreadth(double bre ){
             breadth = bre;}publicvoid setHeight(double hei ){
             height = hei;}// 重载 + 运算符来把两个 Box 对象相加publicstaticBoxoperator+(Box b,Box c){Box box =newBox();
             box.length = b.length + c.length;
             box.breadth = b.breadth + c.breadth;
             box.height = b.height + c.height;return box;}}classTester{staticvoidMain(string[] args){BoxBox1=newBox();// 声明 Box1,类型为 BoxBoxBox2=newBox();// 声明 Box2,类型为 BoxBoxBox3=newBox();// 声明 Box3,类型为 Boxdouble volume =0.0;// 体积// Box1 详述Box1.setLength(6.0);Box1.setBreadth(7.0);Box1.setHeight(5.0);// Box2 详述Box2.setLength(12.0);Box2.setBreadth(13.0);Box2.setHeight(10.0);// Box1 的体积
             volume =Box1.getVolume();Console.WriteLine("Box1 的体积: {0}", volume);// Box2 的体积
             volume =Box2.getVolume();Console.WriteLine("Box2 的体积: {0}", volume);// 把两个对象相加Box3=Box1+Box2;// Box3 的体积
             volume =Box3.getVolume();Console.WriteLine("Box3 的体积: {0}", volume);Console.ReadKey();}}}

    当上面的代码被编译和执行时,它会产生下列结果:

    Box1 的体积: 210
    Box2 的体积: 1560
    Box3 的体积: 5400
    

    可重载和不可重载运算符

    下表描述了 C# 中运算符重载的能力:

    运算符描述
    +, -, !, ~, ++, -- 这些一元运算符只有一个操作数,且可以被重载。
    +, -, *, /, % 这些二元运算符带有两个操作数,且可以被重载。
    ==, !=, <, >, <=, >= 这些比较运算符可以被重载。
    &&, || 这些条件逻辑运算符不能被直接重载。
    +=, -=, *=, /=, %= 这些赋值运算符不能被重载。
    =, ., ?:, ->, new, is, sizeof, typeof 这些运算符不能被重载。

     

    2、

    C# 命名空间(Namespace)

    命名空间的设计目的是提供一种让一组名称与其他名称分隔开的方式。在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突。

    定义命名空间

    命名空间的定义是以关键字 namespace 开始,后跟命名空间的名称,如下所示:

    namespace namespace_name
    {
       // 代码声明
    }
    

    为了调用支持命名空间版本的函数或变量,会把命名空间的名称置于前面,如下所示:

    namespace_name.item_name;

    下面的程序演示了命名空间的用法:

    using System;
    namespace first_space
    {
       class namespace_cl
       {
          public void func()
          {
             Console.WriteLine("Inside first_space");
          }
       }
    }
    namespace second_space
    {
       class namespace_cl
       {
          public void func()
          {
             Console.WriteLine("Inside second_space");
          }
       }
    }   
    class TestClass
    {
       static void Main(string[] args)
       {
          first_space.namespace_cl fc = new first_space.namespace_cl();
          second_space.namespace_cl sc = new second_space.namespace_cl();
          fc.func();
          sc.func();
          Console.ReadKey();
       }
    }

    当上面的代码被编译和执行时,它会产生下列结果:

    Inside first_space
    Inside second_space
    

    using 关键字

    using 关键字表明程序使用的是给定命名空间中的名称。例如,我们在程序中使用 System 命名空间,其中定义了类 Console。我们可以只写:

    Console.WriteLine ("Hello there");

    我们可以写完全限定名称,如下:

    System.Console.WriteLine("Hello there");

    您也可以使用 using 命名空间指令,这样在使用的时候就不用在前面加上命名空间名称。该指令告诉编译器随后的代码使用了指定命名空间中的名称。下面的代码演示了命名空间的应用。

    让我们使用 using 指定重写上面的实例:

    using System;
    using first_space;
    using second_space;
    
    namespace first_space
    {
       class abc
       {
          public void func()
          {
             Console.WriteLine("Inside first_space");
          }
       }
    }
    namespace second_space
    {
       class efg
       {
          public void func()
          {
             Console.WriteLine("Inside second_space");
          }
       }
    }   
    class TestClass
    {
       static void Main(string[] args)
       {
          abc fc = new abc();
          efg sc = new efg();
          fc.func();
          sc.func();
          Console.ReadKey();
       }
    }

    当上面的代码被编译和执行时,它会产生下列结果:

    Inside first_space
    Inside second_space
    

    嵌套命名空间

    命名空间可以被嵌套,即您可以在一个命名空间内定义另一个命名空间,如下所示:

    namespace namespace_name1 
    {
       // 代码声明
       namespace namespace_name2 
       {
         // 代码声明
       }
    }

    您可以使用点(.)运算符访问嵌套的命名空间的成员,如下所示:

    using System;
    using first_space;
    using first_space.second_space;
    
    namespace first_space
    {
       class abc
       {
          public void func()
          {
             Console.WriteLine("Inside first_space");
          }
       }
       namespace second_space
       {
          class efg
          {
             public void func()
             {
                Console.WriteLine("Inside second_space");
             }
          }
       }   
    }
     
    class TestClass
    {
       static void Main(string[] args)
       {
          abc fc = new abc();
          efg sc = new efg();
          fc.func();
          sc.func();
          Console.ReadKey();
       }
    }

    当上面的代码被编译和执行时,它会产生下列结果:

    Inside first_space
    Inside second_space



    3、

    C# 预处理器指令

    预处理器指令指导编译器在实际编译开始之前对信息进行预处理。

    所有的预处理器指令都是以 # 开始。且在一行上,只有空白字符可以出现在预处理器指令之前。预处理器指令不是语句,所以它们不以分号(;)结束。

    C# 编译器没有一个单独的预处理器,但是,指令被处理时就像是有一个单独的预处理器一样。在 C# 中,预处理器指令用于在条件编译中起作用。与 C 和 C++ 不同指令不用,它们不是用来创建宏。一个预处理器指令必须是该行上的唯一指令。

    C# 预处理器指令列表

    下表列出了 C# 中可用的预处理器指令:

    预处理器指令描述
    #define 它用于定义一系列成为符号的字符。
    #undef 它用于取消定义符号。
    #if 它用于测试符号是否为真。
    #else 它用于创建复合条件指令,与 #if 一起使用。
    #elif 它用于创建复合条件指令。
    #endif 指定一个条件指令的结束。
    #line 它可以让您修改编译器的行数以及(可选地)输出错误和警告的文件名。
    #error 它允许从代码的指定位置生成一个错误。
    #warning 它允许从代码的指定位置生成一级警告。
    #region 它可以让您在使用 Visual Studio Code Editor 的大纲特性时,指定一个可展开或折叠的代码块。
    #endregion 它标识着 #region 块的结束。

    #define 预处理器

    #define 预处理器指令创建符号常量。

    #define 允许您定义一个符号,这样,通过使用符号作为传递给 #if 指令的表达式,表达式将返回 true。它的语法如下:

    #define symbol

    下面的程序说明了这点:

    #define PI 
    using System;
    namespace PreprocessorDAppl
    {
       class Program
       {
          static void Main(string[] args)
          {
             #if (PI)
                Console.WriteLine("PI is defined");
             #else
                Console.WriteLine("PI is not defined");
             #endif
             Console.ReadKey();
          }
       }
    }

    当上面的代码被编译和执行时,它会产生下列结果:

    PI is defined
    

    条件指令

    您可以使用 #if 指令来创建一个条件指令。条件指令用于测试符号是否为真。如果为真,编译器会执行 #if 和下一个指令之间的代码。

    条件指令的语法:

    #if symbol [operator symbol]...

    其中,symbol 是要测试的符号名称。您也可以使用 true 和 false,或在符号前放置否定运算符。

    运算符符号是用于评价符号的运算符。可以运算符可以是下列运算符之一:

    • == (equality)
    • != (inequality)
    • && (and)
    • || (or)

    您也可以用括号把符号和运算符进行分组。条件指令用于在调试版本或编译指定配置时编译代码。一个以 #if 指令开始的条件指令,必须显示地以一个 #endif 指令终止。

    下面的程序演示了条件指令的用法:

    #define DEBUG
    #define VC_V10
    using System;
    public class TestClass
    {
       public static void Main()
       {
    
          #if (DEBUG && !VC_V10)
             Console.WriteLine("DEBUG is defined");
          #elif (!DEBUG && VC_V10)
             Console.WriteLine("VC_V10 is defined");
          #elif (DEBUG && VC_V10)
             Console.WriteLine("DEBUG and VC_V10 are defined");
          #else
             Console.WriteLine("DEBUG and VC_V10 are not defined");
          #endif
          Console.ReadKey();
       }
    }

    当上面的代码被编译和执行时,它会产生下列结果:

    DEBUG and VC_V10 are defined



    4、

    C# 正则表达式

    正则表达式 是一种匹配输入文本的模式。.Net 框架提供了允许这种匹配的正则表达式引擎。模式由一个或多个字符、运算符和结构组成。

    定义正则表达式

    下面列出了用于定义正则表达式的各种类别的字符、运算符和结构。

    • 字符转义
    • 字符类
    • 定位点
    • 分组构造
    • 限定符
    • 反向引用构造
    • 备用构造
    • 替换
    • 杂项构造

    字符转义

    正则表达式中的反斜杠字符()指示其后跟的字符是特殊字符,或应按原义解释该字符。

    下表列出了转义字符:

    转义字符描述模式匹配
    a 与报警 (bell) 符 u0007 匹配。 a "Warning!" + 'u0007' 中的 "u0007"
     在字符类中,与退格键 u0008 匹配。 []{3,} "" 中的 ""
    与制表符 u0009 匹配。 (w+) "Name Addr " 中的 "Name " 和 "Addr "
    与回车符 u000D 匹配。( 与换行符 不是等效的。) (w+) " Hello World." 中的 " Hello"
    v 与垂直制表符 u000B 匹配。 [v]{2,} "vvv" 中的 "vvv"
    f 与换页符 u000C 匹配。 [f]{2,} "fff" 中的 "fff"
    与换行符 u000A 匹配。 (w+) " Hello World." 中的 " Hello"
    e 与转义符 u001B 匹配。 e "x001B" 中的 "x001B"
    nnn 使用八进制表示形式指定一个字符(nnn 由二到三位数字组成)。 w40w "a bc d" 中的 "a b" 和 "c d"
    x nn 使用十六进制表示形式指定字符(nn 恰好由两位数字组成)。 wx20w "a bc d" 中的 "a b" 和 "c d"
    c X c x 匹配 X 或 x 指定的 ASCII 控件字符,其中 X 或 x 是控件字符的字母。 cC "x0003" 中的 "x0003" (Ctrl-C)
    u nnnn 使用十六进制表示形式匹配一个 Unicode 字符(由 nnnn 表示的四位数)。 wu0020w "a bc d" 中的 "a b" 和 "c d"
    在后面带有不识别的转义字符时,与该字符匹配。 d+[+-x*]d+d+[+-x*d+ "(2+2) * 3*9" 中的 "2+2" 和 "3*9"

    字符类

    字符类与一组字符中的任何一个字符匹配。

    下表列出了字符类:

    字符类描述模式匹配
    [character_group] 匹配 character_group 中的任何单个字符。 默认情况下,匹配区分大小写。 [mn] "mat" 中的 "m","moon" 中的 "m" 和 "n"
    [^character_group] 非:与不在 character_group 中的任何单个字符匹配。 默认情况下,character_group 中的字符区分大小写。 [^aei] "avail" 中的 "v" 和 "l"
    [ first - last ] 字符范围:与从 first 到 last 的范围中的任何单个字符匹配。 (w+) "Name Addr " 中的 "Name " 和 "Addr "
    . 通配符:与除 之外的任何单个字符匹配。
    若要匹配原意句点字符(. 或 u002E),您必须在该字符前面加上转义符 (.)。
    a.e "have" 中的 "ave", "mate" 中的 "ate"
    p{ name } 与 name 指定的 Unicode 通用类别或命名块中的任何单个字符匹配。 p{Lu} "City Lights" 中的 "C" 和 "L"
    P{ name } 与不在 name 指定的 Unicode 通用类别或命名块中的任何单个字符匹配。 P{Lu} "City" 中的 "i"、 "t" 和 "y"
    w 与任何单词字符匹配。 w "Room#1" 中的 "R"、 "o"、 "m" 和 "1"
    W 与任何非单词字符匹配。 W "Room#1" 中的 "#"
    s 与任何空白字符匹配。 ws "ID A1.3" 中的 "D "
    S 与任何非空白字符匹配。 sS "int __ctr" 中的 " _"
    d 与任何十进制数字匹配。 d "4 = IV" 中的 "4"
    D 匹配不是十进制数的任意字符。 D "4 = IV" 中的 " "、 "="、 " "、 "I" 和 "V"

    定位点

    定位点或原子零宽度断言会使匹配成功或失败,具体取决于字符串中的当前位置,但它们不会使引擎在字符串中前进或使用字符。

    下表列出了定位点:

    断言描述模式匹配
    ^ 匹配必须从字符串或一行的开头开始。 ^d{3} "567-777-" 中的 "567"
    $ 匹配必须出现在字符串的末尾或出现在行或字符串末尾的   之前。 -d{4}$ "8-12-2012" 中的 "-2012"
    A 匹配必须出现在字符串的开头。 Aw{4} "Code-007-" 中的 "Code"
     匹配必须出现在字符串的末尾或出现在字符串末尾的   之前。 -d{3} "Bond-901-007" 中的 "-007"
    z 匹配必须出现在字符串的末尾。 -d{3}z "-901-333" 中的 "-333"
    G 匹配必须出现在上一个匹配结束的地方。 \G(d) "(1)(3)(5)[7](9)" 中的 "(1)"、 "(3)" 和 "(5)"
     匹配一个单词边界,也就是指单词和空格间的位置。 er 匹配"never"中的"er",但不能匹配"verb"中的"er"。
    B 匹配非单词边界。 erB 匹配"verb"中的"er",但不能匹配"never"中的"er"。

    分组构造

    分组构造描述了正则表达式的子表达式,通常用于捕获输入字符串的子字符串。

    下表列出了分组构造:

    分组构造描述模式匹配
    ( subexpression ) 捕获匹配的子表达式并将其分配到一个从零开始的序号中。 (w)1 "deep" 中的 "ee"
    (?< name >subexpression) 将匹配的子表达式捕获到一个命名组中。 (?< double>w)k< double> "deep" 中的 "ee"
    (?< name1 -name2 >subexpression) 定义平衡组定义。 (((?'Open'()[^()]*)+((?'Close-Open'))[^()]*)+)*(?(Open)(?!))$ "3+2^((1-3)*(3-1))" 中的 "((1-3)*(3-1))"
    (?: subexpression) 定义非捕获组。 Write(?:Line)? "Console.WriteLine()" 中的 "WriteLine"
    (?imnsx-imnsx:subexpression) 应用或禁用 subexpression 中指定的选项。 Ad{2}(?i:w+) "A12xl A12XL a12xl" 中的 "A12xl" 和 "A12XL"
    (?= subexpression) 零宽度正预测先行断言。 w+(?=.) "He is. The dog ran. The sun is out." 中的 "is"、 "ran" 和 "out"
    (?! subexpression) 零宽度负预测先行断言。 (?!un)w+ "unsure sure unity used" 中的 "sure" 和 "used"
    (?< =subexpression) 零宽度正回顾后发断言。 (?<=19)d{2} "1851 1999 1950 1905 2003" 中的 "99"、"50"和 "05"
    (?< ! subexpression) 零宽度负回顾后发断言。 (?< !19)d{2} "end sends endure lender" 中的 "ends" 和 "ender"
    (?> subexpression) 非回溯(也称为"贪婪")子表达式。 [13579](?>A+B+) "1ABB 3ABBC 5AB 5AC" 中的 "1ABB"、 "3ABB" 和 "5AB"

    实例

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static void Main()
       {
          string input = "1851 1999 1950 1905 2003";
          string pattern = @"(?<=19)d{2}";
    
          foreach (Match match in Regex.Matches(input, pattern))
             Console.WriteLine(match.Value);
       }
    }
    

    运行实例 »

    限定符

    限定符指定在输入字符串中必须存在上一个元素(可以是字符、组或字符类)的多少个实例才能出现匹配项。 限定符包括下表中列出的语言元素。

    下表列出了限定符:

    限定符描述模式匹配
    * 匹配上一个元素零次或多次。 d*.d ".0"、 "19.9"、 "219.9"
    + 匹配上一个元素一次或多次。 "be+" "been" 中的 "bee", "bent" 中的 "be"
    ? 匹配上一个元素零次或一次。 "rai?n" "ran"、 "rain"
    { n } 匹配上一个元素恰好 n 次。 ",d{3}" "1,043.6" 中的 ",043", "9,876,543,210" 中的 ",876"、 ",543" 和 ",210"
    { n ,} 匹配上一个元素至少 n 次。 "d{2,}" "166"、 "29"、 "1930"
    { n , m } 匹配上一个元素至少 n 次,但不多于 m 次。 "d{3,5}" "166", "17668", "193024" 中的 "19302"
    *? 匹配上一个元素零次或多次,但次数尽可能少。 d*?.d ".0"、 "19.9"、 "219.9"
    +? 匹配上一个元素一次或多次,但次数尽可能少。 "be+?" "been" 中的 "be", "bent" 中的 "be"
    ?? 匹配上一个元素零次或一次,但次数尽可能少。 "rai??n" "ran"、 "rain"
    { n }? 匹配前导元素恰好 n 次。 ",d{3}?" "1,043.6" 中的 ",043", "9,876,543,210" 中的 ",876"、 ",543" 和 ",210"
    { n ,}? 匹配上一个元素至少 n 次,但次数尽可能少。 "d{2,}?" "166"、 "29" 和 "1930"
    { n , m }? 匹配上一个元素的次数介于 n 和 m 之间,但次数尽可能少。 "d{3,5}?" "166", "17668", "193024" 中的 "193" 和 "024"

    反向引用构造

    反向引用允许在同一正则表达式中随后标识以前匹配的子表达式。

    下表列出了反向引用构造:

    反向引用构造描述模式匹配
    number 反向引用。 匹配编号子表达式的值。 (w)1 "seek" 中的 "ee"
    k< name > 命名反向引用。 匹配命名表达式的值。 (?< char>w)k< char> "seek" 中的 "ee"

    备用构造

    备用构造用于修改正则表达式以启用 either/or 匹配。

    下表列出了备用构造:

    备用构造描述模式匹配
    | 匹配以竖线 (|) 字符分隔的任何一个元素。 th(e|is|at) "this is the day. " 中的 "the" 和 "this"
    (?( expression )yes | no ) 如果正则表达式模式由 expression 匹配指定,则匹配 yes;否则匹配可选的 no 部分。 expression 被解释为零宽度断言。 (?(A)Ad{2}|d{3}) "A10 C103 910" 中的 "A10" 和 "910"
    (?( name )yes | no ) 如果 name 或已命名或已编号的捕获组具有匹配,则匹配 yes;否则匹配可选的 no (?< quoted>")?(?(quoted).+?"|S+s) "Dogs.jpg "Yiska playing.jpg"" 中的 Dogs.jpg 和 "Yiska playing.jpg"

    替换

    替换是替换模式中使用的正则表达式。

    下表列出了用于替换的字符:

    字符描述模式替换模式输入字符串结果字符串
    $number 替换按组 number 匹配的子字符串。 (w+)(s)(w+) $3$2$1 "one two" "two one"
    ${name} 替换按命名组 name 匹配的子字符串。 (?< word1>w+)(s)(?< word2>w+) ${word2} ${word1} "one two" "two one"
    $$ 替换字符"$"。 (d+)s?USD $$$1 "103 USD" "$103"
    $& 替换整个匹配项的一个副本。 ($*(d*(.+d+)?){1}) **$& "$1.30" "**$1.30**"
    $` 替换匹配前的输入字符串的所有文本。 B+ $` "AABBCC" "AAAACC"
    $' 替换匹配后的输入字符串的所有文本。 B+ $' "AABBCC" "AACCCC"
    $+ 替换最后捕获的组。 B+(C+) $+ "AABBCCDD" AACCDD
    $_ 替换整个输入字符串。 B+ $_ "AABBCC" "AAAABBCCCC"

    杂项构造

    下表列出了各种杂项构造:

    构造描述实例
    (?imnsx-imnsx) 在模式中间对诸如不区分大小写这样的选项进行设置或禁用。 A(?i)bw+ 匹配 "ABA Able Act" 中的 "ABA" 和 "Able"
    (?#注释) 内联注释。该注释在第一个右括号处终止。 A(?#匹配以A开头的单词)w+
    [行尾] 该注释以非转义的 # 开头,并继续到行的结尾。 (?x)Aw+#匹配以 A 开头的单词
    
    

    Regex 类

    Regex 类用于表示一个正则表达式。

    下表列出了 Regex 类中一些常用的方法:

    序号方法 & 描述
    1 public bool IsMatch( string input ) 
    指示 Regex 构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项。
    2 public bool IsMatch( string input, int startat ) 
    指示 Regex 构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项,从字符串中指定的开始位置开始。
    3 public static bool IsMatch( string input, string pattern ) 
    指示指定的正则表达式是否在指定的输入字符串中找到匹配项。
    4 public MatchCollection Matches( string input ) 
    在指定的输入字符串中搜索正则表达式的所有匹配项。
    5 public string Replace( string input, string replacement ) 
    在指定的输入字符串中,把所有匹配正则表达式模式的所有匹配的字符串替换为指定的替换字符串。
    6 public string[] Split( string input ) 
    把输入字符串分割为子字符串数组,根据在 Regex 构造函数中指定的正则表达式模式定义的位置进行分割。

    如需了解 Regex 类的完整的属性列表,请参阅微软的 C# 文档。

    实例 1

    下面的实例匹配了以 'S' 开头的单词:

    using System;
    using System.Text.RegularExpressions;
    
    namespace RegExApplication
    {
       class Program
       {
          private static void showMatch(string text, string expr)
          {
             Console.WriteLine("The Expression: " + expr);
             MatchCollection mc = Regex.Matches(text, expr);
             foreach (Match m in mc)
             {
                Console.WriteLine(m);
             }
          }
          static void Main(string[] args)
          {
             string str = "A Thousand Splendid Suns";
    
             Console.WriteLine("Matching words that start with 'S': ");
             showMatch(str, @"SS*");
             Console.ReadKey();
          }
       }
    }
    

    当上面的代码被编译和执行时,它会产生下列结果:

    Matching words that start with 'S':
    The Expression: SS*
    Splendid
    Suns
    

    实例 2

    下面的实例匹配了以 'm' 开头以 'e' 结尾的单词:

    using System;
    using System.Text.RegularExpressions;
    
    namespace RegExApplication
    {
       class Program
       {
          private static void showMatch(string text, string expr)
          {
             Console.WriteLine("The Expression: " + expr);
             MatchCollection mc = Regex.Matches(text, expr);
             foreach (Match m in mc)
             {
                Console.WriteLine(m);
             }
          }
          static void Main(string[] args)
          {
             string str = "make maze and manage to measure it";
    
             Console.WriteLine("Matching words start with 'm' and ends with 'e':");
             showMatch(str, @"mS*e");
             Console.ReadKey();
          }
       }
    }
    

    当上面的代码被编译和执行时,它会产生下列结果:

    Matching words start with 'm' and ends with 'e':
    The Expression: mS*e
    make
    maze
    manage
    measure
    

    实例 3

    下面的实例替换掉多余的空格:

    using System;
    using System.Text.RegularExpressions;
    
    namespace RegExApplication
    {
       class Program
       {
          static void Main(string[] args)
          {
             string input = "Hello   World   ";
             string pattern = "\s+";
             string replacement = " ";
             Regex rgx = new Regex(pattern);
             string result = rgx.Replace(input, replacement);
    
             Console.WriteLine("Original String: {0}", input);
             Console.WriteLine("Replacement String: {0}", result);    
             Console.ReadKey();
          }
       }
    }
    

    当上面的代码被编译和执行时,它会产生下列结果:

    Original String: Hello   World   
    Replacement String: Hello World   

     

  • 相关阅读:
    QTP的那些事右键点击对象的方法DeviceReplay
    QTP的那些事时间格式转换函数
    QTP的那些事DOM和childItem(row,column,micclass,index)
    QTP的那些事有关一个webtable数据的获取案例
    QTP的那些事webtable的一些重要使用集合精解
    QTP的那些事有关的一些重要可用的函数(发送邮件)
    ImportSheet in QTP Data Table from QC
    QTP的那些事执行用例后提交bug到QC中
    QTP的那些事一些需要记住的杂谈实践经验
    QTP的那些事报表自定义(excel,html,xml或者是其他格式的)
  • 原文地址:https://www.cnblogs.com/wshyj/p/6344788.html
Copyright © 2020-2023  润新知