【问题标题】:VB6 to C#: Loop or Index Issue?VB6 到 C#:循环或索引问题?
【发布时间】:2013-04-22 04:26:06
【问题描述】:

我有一个旧的 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,它返回了正确的字符串。感谢您的投入。

【问题讨论】:

  • 在得到帮助后,我仔细看了看。我在 Utils.Left/Utils.Right 函数上切换了 +1/-1,它正在工作。我有一种感觉是索引问题。谢谢大家的帮助!
  • 如果我错了,请纠正我,但这个问题与标题无关。
  • +1 完全纠正了这个错误。我会改的。

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


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    尝试使用以下

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

    【讨论】:

    • 不幸的是,这导致了同样的结果。
    【解决方案3】:

    为什么不导入microsoft.visualbasic namespace

    class Program
    {
        static void Main(string[] args)
        {
            string t = Microsoft.VisualBasic.Strings.Mid("hello", 2);
        }
    }
    

    你可以重复使用你以前拥有的东西

    【讨论】:

    • 我想尽量避免这种情况。无论哪种方式,感谢您的输入!如果您阅读我的评论,这已经解决了。我只是不想回答我自己的问题并收到不幸的标准反对票和“这应该在评论或编辑中”cmets。
    猜你喜欢
    • 2016-03-22
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    相关资源
    最近更新 更多