This is the Delphi code I’m trying to convert to .net:
s1 := Copy ( s1 , 1,x - 1) + Copy(s1, x + 1,Length(s1));
I tried:
s1 = s1.Substring(x - 1, 1) + s1.Substring(s1.Length, x + 1)
But I get error’s when the index is out of range. in Delphi it works fine.
Added one line to convert..
s2 := s2 + chr(3);
Your parameters to
Substringare reversed–the start index comes first just as in Delphi.Delphi string indexing is 1-based. The .net string indexing is 0-based. You have the classic off-by-one error.
Finally, you cannot play so loose with the length parameter to
Substring. In Delphi’sCopyyou can specify an arbitrarily large length value and you will get all the right-most characters. InSubstringyou must not ask for more characters than there are. If you do thenArgumentOutOfRangeExceptionis thrown.You need this:
I’m assuming you have already ensured that
xis in the range0tos1.Length-1.As for your additional question,
translates to