我在我的C#代碼如下方法:這段代碼如何可能:「ArgumentOutOfRangeException:startIndex不能大於字符串的長度」?
/// <summary>
/// Removes the first (leftmost) occurence of a <paramref name="substring"/> from a <paramref name="string"/>.
/// </summary>
/// <param name="string">The string to remove the <paramref name="substring"/> from. Cannot be <c>null</c>.</param>
/// <param name="substring">The substring to look for and remove from the <paramref name="string"/>. Cannot be <c>null</c>.</param>
/// <returns>
/// The rest of the <paramref name="string"/>, after the first (leftmost) occurence of the <paramref name="substring"/> in it (if any) has been removed.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item>If the <paramref name="substring"/> does not occur within the <paramref name="string"/>, the <paramref name="string"/> is returned intact.</item>
/// <item>If the <paramref name="substring"/> has exactly one occurence within the <paramref name="string"/>, that occurence is removed, and the rest of the <paramref name="string"/> is returned.</item>
/// <item>If the <paramref name="substring"/> has several occurences within the <paramref name="substring"/>, the first (leftmost) occurence is removed, and the rest of the <paramref name="string"/> is returned.</item>
/// </list>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// The <paramref name="string"/> is <c>null</c>. -or- The <paramref name="substring"/> is <c>null</c>.
/// </exception>
public static string RemoveSubstring(string @string, string substring)
{
if (@string == null)
throw new ArgumentNullException("string");
if (substring == null)
throw new ArgumentNullException("substring");
var index = @string.IndexOf(substring);
return index == -1
? @string
: @string.Substring(0, index) + @string.Substring(index + substring.Length);
}
的實現看起來很淺顯,並通過單元測試有一個出色的網絡覆蓋。在我的計算機,構建服務器或任何其他有權訪問的計算機上,或在大多數生產環境中,都沒有發生意外的結果。
除了只有一個遠程用戶偶爾在此方法具有以下堆棧跟蹤報告應用程序崩潰:
System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string.
Parameter name: startIndex
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex)
at MyNamespace.StringUtils.RemoveSubstring(String string, String substring)
at ...
不幸的是,我沒有這樣的生產環境或將其數據的遠程訪問,或到任何額外的信息。出於某些原因,目前我無法在那裏部署日誌記錄系統或崩潰轉儲收集。在代碼
展望,並嘗試的參數不同的組合,我無法想象怎麼可能會出現此異常。
能否請你幫我的一些想法?
的IndexOf是特定的文化。查看示例[此處](http://msdn.microsoft.com/zh-cn/library/ms224425.aspx),查看索引如何更改。 –
使用String.Remove(Int32,Int32)方法可能會更好。你可能會發現它更寬容。 ':@ string.Remove(index,substring.Length)' – tinstaafl