博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 2.0学习之--集合1
阅读量:7212 次
发布时间:2019-06-29

本文共 1501 字,大约阅读时间需要 5 分钟。

 编译与执行:
csc tokens.cs            tokens

tokens.cs :
//Copyright (C) Microsoft Corporation.  All rights reserved.

// tokens.cs

using System;
// The System.Collections namespace is made available:
using System.Collections;

// Declare the Tokens class:

public class Tokens : IEnumerable
{
   private string[] elements;

   Tokens(string source, char[] delimiters)

   {
      // Parse the string into tokens:
      elements = source.Split(delimiters);
   }

   // IEnumerable Interface Implementation:

   //   Declaration of the GetEnumerator() method
   //   required by IEnumerable
   public IEnumerator GetEnumerator()
   {
      return new TokenEnumerator(this);
   }

   // Inner class implements IEnumerator interface:

   private class TokenEnumerator : IEnumerator
   {
      private int position = -1;
      private Tokens t;

      public TokenEnumerator(Tokens t)

      {
         this.t = t;
      }

      // Declare the MoveNext method required by IEnumerator:

      public bool MoveNext()
      {
         if (position < t.elements.Length - 1)
         {
            position++;
            return true;
         }
         else
         {
            return false;
         }
      }

      // Declare the Reset method required by IEnumerator:

      public void Reset()
      {
         position = -1;
      }

      // Declare the Current property required by IEnumerator:

      public object Current
      {
         get
         {
            return t.elements[position];
         }
      }
   }

   // Test Tokens, TokenEnumerator

   static void Main()

   {
      // Testing Tokens by breaking the string into tokens:
      Tokens f = new Tokens("This is a well-done program.",
         new char[] {' ','-'});
      foreach (string item in f)
      {
         Console.WriteLine(item);
      }
   }
}

 

转载于:https://www.cnblogs.com/llbofchina/archive/2006/06/23/434152.html

你可能感兴趣的文章
使用Netsil监控Kubernetes上的微服务
查看>>
小米正式开源 Istio 管理面板 Naftis
查看>>
Zabbix 添加WEB监控(学习笔记十一)
查看>>
ImageIO 框架详细解析
查看>>
查询一个ID出现2种结果的情况
查看>>
亚马逊 OpenJDK 发行版 Corretto GA
查看>>
kaldi 源码分析(七) - HCLG 分析
查看>>
SpaceVim 1.1.0 发布,模块化 Vim IDE
查看>>
Java 设计模式六大原则
查看>>
CentOS7 搭建Ambari-Server,安装Hadoop集群(一)
查看>>
Python爬虫基础:验证码的爬取和识别详解
查看>>
WPF 可触摸移动的ScrollViewer控件
查看>>
mysql事务
查看>>
HBase基本操作-java api
查看>>
PostgreSQL的时间/日期函数使用
查看>>
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 12 章 全文搜索_12.4. 额外特性
查看>>
十字星文化获数千万元A轮融资,腾讯持续下注
查看>>
cron和crontab
查看>>
从阿里云数据库入选Gartner谈数据库的演化
查看>>
【Unity Shader】(六) ------ 复杂的光照(上)
查看>>