首页 > 开发 > 综合 > 正文

几个C# PROGRAMS (2)

2024-07-21 02:17:51
字体:
来源:转载
供稿:网友
 

using system;
using system.text.regularexpressions;

namespace wrox.procsharp.regularexpressionplayaround
{
   class mainentrypoint
   {
      static void main()
      {
         find1();
         console.readline();
      }

      static void find1()
      {
         string text = @"xml has made a major impact in almost every aspect of
            software development. designed as an open, extensible, self-describing
            language, it has become the standard for data and document delivery on
            the web. the panoply of xml-related technologies continues to develop
            at breakneck speed, to enable validation, navigation, transformation,
            linking, querying, description, and messaging of data.";
         string pattern = @"/bn/s*ion/b";
         matchcollection matches = regex.matches(text, pattern,
            regexoptions.ignorecase | regexoptions.ignorepatternwhitespace |
            regexoptions.explicitcapture);
         writematches(text, matches);
      }  

      static void find2()
      {
         string text = @"xml has made a major impact in almost every aspect of
            software development. designed as an open, extensible, self-describing
            language, it has become the standard for data and document delivery on
            the web. the panoply of xml-related technologies continues to develop
            at breakneck speed, to enable validation, navigation, transformation,
            linking, querying, description, and messaging of data.";
         string pattern = @"/bn";
         matchcollection matches = regex.matches(text, pattern,
           regexoptions.ignorecase);
         writematches(text, matches);
      }

      static void writematches(string text, matchcollection matches)
      {
         console.writeline("original text was: /n/n" + text + "/n");
         console.writeline("no. of matches: " + matches.count);
         foreach (match nextmatch in matches)
         {
            int index = nextmatch.index;
            string result = nextmatch.tostring();
            int charsbefore = (index < 5) ? index : 5;
            int fromend = text.length - index - result.length;
            int charsafter = (fromend < 5) ? fromend : 5;
            int charstodisplay = charsbefore + charsafter + result.length;

            console.writeline("index: {0}, /tstring: {1}, /t{2}",
               index, result,
               text.substring(index - charsbefore, charstodisplay));

         }
      }
   }
}

using system;
using system.text;

namespace wrox.procsharp.stringencoder
{
   class mainentrypoint
   {
      static void main(string[] args)
      {
         string greetingtext = "hello from all the guys at wrox press. ";
         greetingtext += "we do hope you enjoy this book as much as we enjoyed writing it.";

         for(int i = (int)'z'; i>=(int)'a' ; i--)
         {
            char old = (char)i;
            char new = (char)(i+1);
            greetingtext = greetingtext.replace(old, new);
         }

         for(int i = (int)'z'; i>=(int)'a' ; i--)
         {
            char old = (char)i;
            char new = (char)(i+1);
            greetingtext = greetingtext.replace(old, new);
         }
         console.writeline("encoded:/n" + greetingtext);

         stringbuilder greetingbuilder =
            new stringbuilder("hello from all the guys at wrox press. ", 150);
         greetingbuilder.append("we do hope you enjoy this book as much as we enjoyed writing it");

         for(int i = (int)'z'; i>=(int)'a' ; i--)
         {
            char old = (char)i;
            char new = (char)(i+1);
            greetingbuilder = greetingbuilder.replace(old, new);
         }

         for(int i = (int)'z'; i>=(int)'a' ; i--)
         {
            char old = (char)i;
            char new = (char)(i+1);
            greetingbuilder = greetingbuilder.replace(old, new);
         }
         console.writeline("encoded:/n" + greetingbuilder.tostring());
     
      }
   }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表