question

moondaddy-8531 avatar image
0 Votes"
moondaddy-8531 asked karenpayneoregon commented

c# String.Substring.Remove exception

using c# 4.8 I get and exception when combining the string Substring and Remove methods.

this is the string I'm working with inside the square brackets (the value of the variable x):

 [        "x": -89.05621501099995,]

and this is my code:

 string test = x.Substring(13).Remove(x.Length-1);

I'm using x.Substring(13) to remove all text the the left of the minus sign.
and I'm using .Remove(x.Length-1) to remove the trailing comma.

But the remove method gives me this exception:

'x.Substring(13).Remove(x.Length-2)' threw an exception of type 'System.ArgumentOutOfRangeException'

Note: this works if I split this into 2 lines of code and add a new variable like this:

 string strX = x.Substring(13);
 strX = strX.Remove(strX.Length - 2);

but I have to apply the same process to a bunch of variables all in a loop with 100s of 1000s of loops server side, so I'm trying to optimize this.

Any ideas on the best way to approach this?

Thanks.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered karenpayneoregon commented

Try another statement:

 string test = x.Remove( x.Length - 1 ).Substring( 13 );

In case of new .NET 5 or Core, this should work too:

 string x = "        \"x\": -89.05621501099995,";
 string test = x[13..^1];

If it is a part of JSON, then maybe use a special parser.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you @karenpayneoregon and @Viorel-1. I'm guessing out of all our options here that:

 string test = x.Remove( x.Length - 1 ).Substring( 13 );

Will be the fastest in a big loop. Please let me know if I'm wrong.

0 Votes 0 ·

It is probably faster to have a single call:

 string test = x.Substring( 13, x.Length - 1 - 13 );

But the final overall performance depends on other part and approaches.

For example, if you want to convert this substring to double, then maybe you can parse the characters directly from the main string, using some special third-party or external functions, avoiding extracting temporary sub-strings. But the gain will be insignificant if other parts are not fast.


0 Votes 0 ·

The issue I see with indexing into a string is what if the position/index changes while that is not the case with regular expressions as it's stripping away unwanted data. If I were concerned about speed in a loop then do a simple Stopwatch elapsed time such as my singleton stopwatch class. Run both several times as usually things will speed up after initial runs.


0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

Try the following

 var someValue = " [        \"x\": -89.05621501099995,]";
 var result = Regex.Replace(someValue, "[^0-9.-]", "");

Array example

 string[] values = {" [        \"x\": -89.05621501099995,]", "", " [        \"x\": -123.05,]" };
    
 foreach (var value in values)
 {
     var fixedValue = Regex.Replace(value, "[^0-9.-]", "");
     Console.WriteLine(string.IsNullOrWhiteSpace(fixedValue) ? "Empty value" : $"{value} -> {fixedValue}");
 }

81661-f1.png



f1.png (13.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.