c# - 这段代码怎么可能: "ArgumentOutOfRangeException: startIndex cannot be larger than length of string"?

标签 c# .net debugging exception substring

我的 C# 代码中有以下方法:

/// <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 ...

不幸的是,我无法远程访问此生产环境或其数据或任何其他信息。由于某些原因,目前我无法在那里部署日志系统或故障转储收集。

查看代码并尝试不同的参数组合,我无法想象这种异常是如何发生的。

你能帮我出点主意吗?

最佳答案

RemoveSubstring("A", "A\uFFFD"); // throws ArgumentOutOfRangeException
RemoveSubstring("A", "A\u0640"); // throws ArgumentOutOfRangeException

.NET 中的许多字符串操作函数,包括 IndexOf 默认情况下是特定于文化的(通常有重载,您可以在其中传递 StringComparison.OrdinalStringComparer.Ordinal 切换到按位比较)。就个人而言,我对被选为默认行为的内容不是很满意,但现在对此做任何事情都为时已晚,除非可能设置明确的开发指南和 FxCop 规则。

但有时特定于文化的操作正是您所需要的。不幸的是,它们的语义可能很棘手且违反直觉,可能违反一些通常假设的不变量,并且有很多极端情况需要处理。负责在应用程序中实现文化敏感逻辑的开发人员应该在这方面非常合格,并且始终准确地了解他们在做什么。我建议为该领域设定高于正常水平的审查和测试标准。

关于c# - 这段代码怎么可能: "ArgumentOutOfRangeException: startIndex cannot be larger than length of string"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17768854/

相关文章:

c# - 如何捕获 JsonConverter 属性中抛出的异常?

c# - 针对 net45 时的 System.Numerics

.net - 缺少 .NET Standard 2.0 Framework 和最新的 .NET Core SDK 2.2.1、.NET Framework 4.7.2、VS2017 15.9.3

c - 如何从 $1 重新开始命名 GDB 的内部变量?

c++ - OpenCV findContours 导致返回时调试断言失败

c# - 事件中心上 IoTHub 数据的概念

c# - 等待已经完成的任务时会发生什么?

c# - 从移动设备访问本地主机上托管的 ASP.NET Core 2.1 Web 应用程序

c# - 由于格式不正确,加载程序集失败

javascript - Google 应用引擎授权返回 null