LastIndexOf()方法参数解释--明白就结贴

kk169cn 2006-11-30 05:15:54
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的取值?
...全文
590 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liujia_0421 2006-11-30
  • 打赏
  • 举报
回复
public int LastIndexOf (
string value,
int startIndex,
int count
)
参数
value
要查找的 String。

startIndex
搜索起始位置。

count
要检查的字符位置数。



返回值
如果找到该字符,则为 value 的索引位置;如果未找到该字符,则为 -1。如果 value 为 Empty,则返回值为 startIndex。

lovefootball 2006-11-30
  • 打赏
  • 举报
回复
LastIndexOf是从右向左取

start是起始位置
count是向左的位数,也就是向左取多少个

比如
string s = "123456789";
Console.WriteLine(s.LastIndexOf("9", 0, 2));//运行报错
dugupiaoyun 2006-11-30
  • 打赏
  • 举报
回复
这个count是指的数目
例如字符串str="abcd"
那么str.LastIndexOf('c', 1, 3)
就代表str索引从1开始取,取3个字符,然后在里面查找字符'c'
也就是LastIndexOf('t', start, count)
count代表取多少个

kk169cn 2006-11-30
  • 打赏
  • 举报
回复
不明白 count 的取值范围 为什么 count=0 什么也取不到

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧