String.IndexOf 方法 (String)
指定字符串在此实例中的第一个匹配项的从零开始的索引
下面的示例使用 IndexOf 方法确定句子中动物名字的起始位置。 然后,该方法在此位置插入一个形容词来描述句子中的动物。
using System;
public class Example {
public static void Main()
{
string animal1 = "fox";
string animal2 = "dog";
string strTarget = String.Format("The {0} jumped over the {1}.",
animal1, animal2);
Console.WriteLine("The original string is:{0}{1}{0}",
Environment.NewLine, strTarget);
Console.Write("Enter an adjective (or group of adjectives) " +
"to describe the {0}: ==> ", animal1);
string adj1 = Console.ReadLine();
Console.Write("Enter an adjective (or group of adjectives) " +
"to describe the {0}: ==> ", animal2);
string adj2 = Console.ReadLine();
adj1 = adj1.Trim() + " ";
adj2 = adj2.Trim() + " ";
strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1);
strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2);
Console.WriteLine("{0}The final string is:{0}{1}",
Environment.NewLine, strTarget);
}
} |