代码之家  ›  专栏  ›  技术社区  ›  MoralesJosue

检查数组C中的空字符#

  •  2
  • MoralesJosue  · 技术社区  · 10 年前

    我正在读取表上的一个字段,它只有3个值(“”、ESD、R&S)

    我不知道为什么,但当我读R&S值,打印输出标签为R(“空白”)S 这是我使用的代码:

    char[] area = read1[8].ToString().ToCharArray();
    // if array is less than one do nothing
    if (area.Length > 1)
    {
        //trying to use this to check if the second item of array is the "&" symbol (print this format data)
        if (area[1].ToString() == "&")
        {
            Arealbl.Text = area[0].ToString() + "\n" + "&" + "\n" + area[2].ToString();
        }
        //else print out this format data
        else
        {
            Arealbl.Text = area[0].ToString() + "\n" + area[1].ToString() + "\n" + area[2].ToString();
        }
    }
    

    我使用这段代码是因为我还没有找到一种简单的方法来在垂直方向上放置标签。

    2 回复  |  直到 10 年前
        1
  •  1
  •   Henk Holterman    10 年前

    这个 & 是MenuItems、Labels和Buttons中的一个特殊字符,用于指示下一个字符应加下划线。当你设法聚焦Arealbl并按下Alt时,你可能会看到这一点。

    设置

    Arealbl.UseMnemonic = false;
    

    在某处就像设计师一样。

        2
  •  1
  •   AWinkle    10 年前

    除了@Henk Holterman的回答,这里还有一些代码审查建议。您可以将字符串作为数组访问,因此不需要 .ToString().ToCharArray() ,只是为了 .ToString() 一切都在方法的后面。简化与 string.Format 可以帮助提高可读性,并且假设您不必多次(数万次)这样做,则不会影响性能。

    string area = read1[8].ToString()
    if(area.Length < 3) { return; } //exit early on error conditions.
    // if array is less than one do nothing
    Arealbl.UseMnemonic = false; //only add this if you cannot guarantee it will be set.
    Arealbl.Text = string.Format("{0}\n{1}\n{2}", area[0], area[1], area[2]);