将delphi的System.Copy转换为.net [英] Convert delphi's System.Copy to .net

查看:52
本文介绍了将delphi的System.Copy转换为.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要转换为.net的Delphi代码:

This is the Delphi code I'm trying to convert to .net:

   s1 := Copy ( s1 , 1,x - 1) + Copy(s1, x + 1,Length(s1));

我尝试过:

  s1 = s1.Substring(x - 1, 1) + s1.Substring(s1.Length, x + 1)

但是当索引超出范围时出现错误.在Delphi中工作正常.

But I get error's when the index is out of range. in Delphi it works fine.

添加了一行以进行转换.s2:= s2 + chr(3);

Added one line to convert.. s2 := s2 + chr(3);

推荐答案

您要设置的参数 Substring 颠倒了,开始索引排在第一位,就像在Delphi中一样.

Your parameters to Substring are reversed–the start index comes first just as in Delphi.

Delphi字符串索引基于1..net字符串索引从0开始.您会遇到经典的一对一"错误.

Delphi string indexing is 1-based. The .net string indexing is 0-based. You have the classic off-by-one error.

最后,对于 Substring 的length参数,您不能玩得那么松散.在Delphi的 Copy 中,您可以指定一个任意大的长度值,您将获得所有最右边的字符.在 Substring 中,您所输入的字符数不能超过要求的数量.如果这样做,则会引发 ArgumentOutOfRangeException .

Finally, you cannot play so loose with the length parameter to Substring. In Delphi's Copy you can specify an arbitrarily large length value and you will get all the right-most characters. In Substring you must not ask for more characters than there are. If you do then ArgumentOutOfRangeException is thrown.

您需要这个:

s1 = s1.Substring(0, x-1) + s1.Substring(x, s1.Length-x)

我假设您已经确保 x 0 s1.Length-1 的范围内.

I'm assuming you have already ensured that x is in the range 0 to s1.Length-1.

关于您的其他问题

s2 := s2 + chr(3);

翻译为

s2 = s2 + Chr(3)

这篇关于将delphi的System.Copy转换为.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆