这是一个简单的实现--我知道有些算法像KMP
然而,我正试图尽我所能地实现它。
string test = "giladdarmonwhatareyoudoing";
int index = StrStr(test, "are");
//Returns an index to the first occurrence of str2 in str1,
//or a -1 pointer if str2 is not part of str1.
public int StrStr(string test, string strToFind)
{
for (int i = 0; i < test.Length; i++)
{
if (test[i] == strToFind[0])
{
int j;
for (j = 0; j < strToFind.Length; j++)
{
if (test[i + j] != strToFind[j])
{
break;
}
}
if (j == strToFind.Length)
{
return i;
}
}
}
return -1;
}发布于 2014-12-16 10:00:25
String.Empty作为第二个参数传递,则抛出一个IndexOutOfRangeExceptionIndexOutOfRangeException字符串test = "giladdarmonwhatareyoudoing";int index = StrStr(test,"ng1");null作为第一个或第二个参数传递,则抛出一个NullReferenceException。在这里,最好使用ArgumentNullException子句抛出一个守护语句。如果(测试==空){抛出新ArgumentNullException(“测试”);} if (strToFind ==空){抛出新ArgumentNullException("strToFind");}strToFind.Length > test.Lengthtest.Length - i > strToFind.Length你不应该用匈牙利符号。考虑将strToFind重命名为searchForm或searchArgument。
将内部循环提取到单独的方法之后,删除现在不需要的if (test[i] == strToFind[0])并实现上面的内容
public int StrStr(string value, string searchArgument)
{
if (value == null) { throw new ArgumentNullException("value"); }
if (searchArgument == null) { throw new ArgumentNullException("searchArgument"); }
if (searchArgument.Length == 0) { return 0; }
int searchLength = searchArgument.Length;
int length = value.Length;
if (searchLength > length) { return -1; }
for (int i = 0; i < length; i++)
{
if (length - i < searchLength) { return -1; }
if (IsMatchAtIndex(value, searchArgument, i)) { return i; }
}
return -1;
}
private bool IsMatchAtIndex(String value, String searchArgument, int startIndex)
{
for (int j = 0; j < searchArgument.Length; j++)
{
if (value[startIndex + j] != searchArgument[j])
{
return false;
}
}
return true;
} 我更喜欢上面的,但你也可以加上
if (length - i < searchLength) { return -1; } 倒置为for循环的条件
for (int i = 0; i < length && length - i >= searchLength; i++)
{
if (IsMatchAtIndex(value, searchArgument, i)) { return i; }
}发布于 2014-12-15 19:45:31
//返回str2在str1中第一次出现的索引,如果str2不是str1的一部分,则返回//或a-1指针。公共int StrStr(string test,string strToFind)
该注释作为XML注释要好得多,这样客户端代码就可以看到使用IntelliSense的方法的文档:
/// <summary>
/// Returns an index to the first occurrence of str2 in str1,
/// or a -1 pointer if str2 is not part of str1.
/// </summary>
public int StrStr(string test, string strToFind)另一个问题是,注释中提到的str1和str2参数不是您使用的实际参数名称。您可以使用paramref:
/// <summary>
/// Finds the index of the first occurrence of <paramref name="test"/> in <paramref name="strToFind"/>.
/// </summary>
/// <param name="test">The input string.</param>
/// <param name="strToFind">The value to find.</param>
/// <returns>Returns -1 if specified value is not found.</returns>
public int StrStr(string test, string strToFind)MSDN:
<paramref>标记为您提供了一种方法来指示代码注释中的单词(例如,在<summary>或<remarks>块中)是指参数。可以处理XML文件,以某种不同的方式格式化这个单词,例如使用粗体或斜体。
使用<returns>标记来指定函数的返回值-- <summary>标记应该简单地说明该函数的功能,而不是涵盖所有可能的结果。
发布于 2014-12-15 19:42:37
主要是一些小事:
StrStr实际上不是一个很好的名称。StartIndexOf或类似的可能更好。string的扩展方法编写,这将产生更好的调用语法。它应该是这样的:公共静态int StartIndexOf(此字符串测试,字符串toFind) {.},并这样称呼它: string test = "giladdarmonwhatareyoudoing";int index = test.StartIndexOf("are");https://codereview.stackexchange.com/questions/73754
复制相似问题