c# - VB6 到 C# : Loop or Index Issue?

标签 c# vb6 indexing while-loop vb6-migration

我有一个旧的 VB6 项目,正在将其转换为 C#。我有一根绳子。我正在验证,一次 1 个字符,它仅包含 [E0-9.+-]

这是旧的 VB6 代码:

sepIndex = 1

Do While Mid(xyString, sepIndex, 1) Like "[E0-9.+-]"
    sepIndex = sepIndex + 1
Loop

然后,我使用索引号来计算同一字符串的Left,并将其转换为 double 型。然后,我使用 Right 将字符串剪切到我想要的字符串的剩余部分:

xFinal = CDbl(left(xyString, sepIndex - 1))
xyString = LTrim(right(xyString, Len(xyString) - sepIndex + 1))

这在 VB 中运行得很好,我没有任何问题。在 C# 中则不同。知道在 C# 中模拟 Left/Mid/Right 的唯一方法是创建我自己的函数,因此我这样做了(我的 Utils 命名空间的一部分):

public static string Mid(string param, int startIndex)
   {
       try
       {

           //start at the specified index and return all characters after it
           //and assign it to a variable
           string result = param.Substring(startIndex);
           //return the result of the operation
           return result;
       }
       catch
       {
           return string.Empty;
       }
   }

public static string Right(string param, int length)
   {
       try
       {
           //start at the index based on the lenght of the sting minus
           //the specified lenght and assign it a variable
           string result = param.Substring(param.Length - length, length);
           //return the result of the operation
           return result;
       }
       catch
       {
           return string.Empty;
       }
   }
public static string Left(string param, int length)
   {
       try
       {
           //we start at 0 since we want to get the characters starting from the
           //left and with the specified lenght and assign it to a variable
           string result = param.Substring(0, length);
           //return the result of the operation
           return result;
       }
       catch
       {
           return string.Empty;
       }
   }

据我所知,我已将代码从 VB6 转换为 C#。问题是,当它最终设置xyString时,它在错误的位置剪切字符串,并且仍然在xFinal中留下我想要的尾随字符。

据我所知,这可能是索引问题(我知道在 C# 中,Substring 是从 0 开始的,因此我将 sepIndex 更改为以下值0),或者循环结构错误的问题。

这是转换后的代码,我希望我能了解这里发生的情况:

sepIndex = 0;

while (Regex.IsMatch(Utils.Mid(xyString, sepIndex, 1), "[E0-9.+-]"))
{
    sepIndex++;
}

xFinal = Convert.ToDouble(Utils.Left(xyString, sepIndex - 1)); // - 1
xyString = Utils.Right(xyString, xyString.Length - sepIndex + 1).TrimStart(' '); // + 1

编辑

这个问题已经解决了。我只是从 Utils.Left 函数中删除了 +1 和 -1,它就返回了正确的字符串。感谢您的投入。

最佳答案

似乎只是带有负条件的正则表达式数学 - “[^E0-9.+-]”:

 var sepIndex = Regex.Match(xyString, @"[^E0-9\.+-]+").Index;

如果您需要手动执行此操作,获取单个字符的最简单方法是仅从字符串中获取单个字符而不进行修剪:

  // replace Utils.Mid(xyString, sepIndex, 1) with
  xyString[sepIndex] 

关于c# - VB6 到 C# : Loop or Index Issue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16139442/

相关文章:

c# - 通用类型参数没有装箱或类型参数转换

c++ - 如何以位字节序将数字转换为字节数组

api - 如何获取显示器的当前方向(角度)?

indexing - 对字符串和 `RangeFull`使用 `Borrow`语法

matlab - 修剪数据以便在对数对数图上更好地查看 - Matlab

c# - WPF MVVM Light 单元测试 ViewModels

c# - 带有脚本的Unity音频错误

C# 正则表达式 : Get sub-capture?

.net - 未找到 DllRegisterServer 入口点 - 注册 Vb.NET DLL 时?

matlab - 如何使用混合索引格式访问 MATLAB 中的多维数组