using System;class Sample {
    public static void Main() {    string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
    string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
    string str = "Now is the time for all good men to come to the aid of their party.";
    int start;
    int at;
    int count;
    int end;    start = str.Length-1;
    end = start/2 - 1;
    Console.WriteLine("All occurences of 't' from position {0} to {1}.", start, end);
    Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
    Console.Write("The letter 't' occurs at position(s): ");    count = 0;
    at = 0;
    while((start > -1) && (at > -1))
        {
        count = start - end; //Count must be within the substring.
        at = str.LastIndexOf('t', start, count);
        if (at > -1) 
            {
            Console.Write("{0} ", at);
            start = at - 1;
            }
        }
    Console.Write("{0}{0}{0}", Environment.NewLine);
    }
}
/*
This example produces the following results:
All occurences of 't' from position 66 to 32.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.The letter 't' occurs at position(s): 64 55 44 41 33我不明白的是  count的取值?

解决方案 »

  1.   

    不明白 count 的取值范围  为什么 count=0 什么也取不到
      

  2.   

    这个count是指的数目
    例如字符串str="abcd"
    那么str.LastIndexOf('c', 1, 3)
    就代表str索引从1开始取,取3个字符,然后在里面查找字符'c'
    也就是LastIndexOf('t', start, count)
    count代表取多少个
      

  3.   

    LastIndexOf是从右向左取start是起始位置
    count是向左的位数,也就是向左取多少个比如
    string s = "123456789";
    Console.WriteLine(s.LastIndexOf("9", 0, 2));//运行报错
      

  4.   

    public int LastIndexOf (
    string value,
    int startIndex,
    int count
    )
    参数
    value
    要查找的 String。 startIndex
    搜索起始位置。 count
    要检查的字符位置数。 返回值
    如果找到该字符,则为 value 的索引位置;如果未找到该字符,则为 -1。如果 value 为 Empty,则返回值为 startIndex。