2013-03-23 66 views

回答

9

你会想在字符串上使用IndexOf函数。这会告诉你你正在寻找的单词,角色等的起始字符位置。

下面是一个例子控制台应用程序:

static void Main(string[] args) 
    { 
     String testing = "text that i am looking for"; 
     Console.Write(testing.IndexOf("looking") + Environment.NewLine); 
     Console.WriteLine(testing.Substring(testing.IndexOf("looking"))); 

     Console.ReadKey(); 

    } 

这将输出:
寻找

+3

只需添加,最好在使用前检查'tests.IndexOf'是否大于'-1',否则如果在字符串中没有找到'looking','Substring'会抛出'ArgumentOutOfRange'异常。 – keyboardP 2013-03-23 01:51:32

+0

良好的调用 - 我在假设在实际使用之前会有更多的数据验证的情况下写这个。 – 2013-03-23 01:52:55

+0

真棒! THANKS!,不知道子字符串:p – 2013-03-23 01:53:40

相关问题