Thiago Simão
Thiago Simão

Reputation: 11

How do I remove specific text in a string using substring

My string is this:

fileName, callDateTime, sum(callDuration) AS 'Call Duration', callRecorder, equipmentId, errorMessage, extractionDateTime, interactionId, interrupted, extractionSuccess, stitching, bytes, conversionTime, stitchingTime, transferTime, remoteErrorMessage, remoteTransferDateTime, size, remoteTimeToTransfer, extractionStartDate, extractionEndDate, purpose, ruleName, ruleSession, fileOutputFormat, extractedBy, vaultName, vaultType, vaultRemotePath, stitchingTime

I want to remove this part of the text:

"sum (callDuration) AS 'Call Duration',"

I am using this piece of code to try to address this problem:

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

But for some reason, he takes this part of the text:

sum(callDuration) AS 'Call Duration', callRec

Upvotes: 0

Views: 61

Answers (1)

David
David

Reputation: 218837

Your variable names imply that you misunderstand how the Substring(int, int) method works. The second argument is not an index, it's a length.

Whether you want to hard-code the length for "sum (callDuration) AS 'Call Duration'," or dynamically calculate it (and how you'd dynamically calculate it) is really up to you. But you need to tell the Substring() method the size of the substring to remove, not the ending index of the substring to remove.

Upvotes: 2

Related Questions