【问题标题】:How do I remove specific text in a string using substring如何使用子字符串删除字符串中的特定文本
【发布时间】:2019-11-19 20:23:27
【问题描述】:

我的字符串是这样的:

fileName, callDateTime, sum(callDuration) AS '通话时长', callRecorder、deviceId、errorMessage、extractionDateTime、 interactionId,中断,extractionSuccess,拼接,字节, 转换时间、拼接时间、传输时间、远程错误消息、 remoteTransferDateTime,大小,remoteTimeToTransfer, extractStartDate, extractEndDate, 目的, ruleName, ruleSession、fileOutputFormat、extractBy、vaultName、vaultType、 vaultRemotePath,拼接时间

我想删除这部分文字:

"sum (callDuration) AS '通话时长',"

我正在使用这段代码来尝试解决这个问题:

 var getSumIndex = resultAggOrField.IndexOf($"{rowGroup.AggFunc}");
 var getFieldIndex = resultAggOrField.IndexOf($"'{rowGroup.DisplayName}'");
 var delete = resultAggOrField.Substring(getSumIndex, getFieldIndex);
 resultAgg = resultAggOrField.Replace(delete, string.Empty);

但由于某种原因,他取了这部分文字:

sum(callDuration) AS 'Call Duration', callRec

【问题讨论】:

  • 调试的时候rowGroup.AggFunc的值是多少? rowGroup.DisplayName 的? getSumIndex的? getFieldIndex的? delete 的?基本上,哪个特定操作会产生意想不到的结果?
  • 删除变量的结果: "sum(callDuration) AS 'Call Duration', callRec"我希望它是:"sum (callDuration) AS 'Call Duration',"

标签: c# string substring


【解决方案1】:

您的变量名称暗示您误解了the Substring(int, int) method 的工作原理。第二个参数不是index,而是length

您是想硬编码"sum (callDuration) AS 'Call Duration'," 的长度还是动态计算它(以及如何动态计算它)完全取决于您。但是您需要告诉Substring() 方法要删除的子字符串的size,而不是要删除的子字符串的结束索引

【讨论】:

    猜你喜欢
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多