日期:2013-01-09  浏览次数:20548 次

// indexedproperty.cs
using System;

public class Document
{
    // Type allowing the document to be viewed like an array of words:
    public class WordCollection
    {
        readonly Document document;  // The containing document

        internal WordCollection(Document d)
        {
           document = d;
        }

        // Helper function -- search character array "text", starting at
        // character "begin", for word number "wordCount." Returns false
        // if there are less than wordCount words. Sets "start" and
        // length" to the position and length of the word within text:
        private bool GetWord(char[] text, int begin, int wordCount,
                                       out int start, out int length)
        {
            int end = text.Length;
            int count = 0;
            int inWord = -1;
            start = length = 0;

            for (int i = begin; i <= end; ++i)
            {
                bool isLetter = i < end && Char.IsLetterOrDigit(text[i]);

                if (inWord >= 0)
                {
                    if (!isLetter)
                    {
                        if (count++ == wordCount)
                        {
                            start = inWord;
                            length = i - inWord;
                            return true;
                        }