String.Indexof 方法

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

// Sample for String.IndexOf(String, Int32, Int32)

// String.Indexof 方法 报告 String 在此实例中的第一个匹配项的索引或一个或多个字符的索引。

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 end;

int count;

end = str.Length;

start = end/2;

Console.WriteLine();

Console.WriteLine("All occurences of 'he' from position {0} to {1}.", start, end-1);

Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);

Console.Write("The string 'he' occurs at position(s): ");

count = 0;

at = 0;

while((start <= end) && (at > -1))

{

// start+count must be a position within -str-.

count = end - start;

at = str.IndexOf("he", start, count);

if (at == -1) break;

Console.Write("{0} ", at);

start = at+1;

}

Console.WriteLine();

}

}

/*

This example produces the following results:

All occurences of 'he' from position 33 to 66.

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 string 'he' occurs at position(s): 45 56

*/

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航