程序如下:            string s = "aaa";
            s += "&";
            label1.Text = s;这段代码执行结果label1应该显示"aaa&"。但实际上却显示"aaa",后面的&竟然没有了。
求解啊C#String字符串操作

解决方案 »

  1.   

    特殊字符转义下不就行了 \& 试试
      

  2.   

    谢谢版主解答
    可是程序运行lable的text确实是aaa呢?
      

  3.   

    谢谢版主解答
    可是程序运行lable的text确实是aaa呢?
    不管转义的事,winform 下是吧,应该不会出现这种问题
      

  4.   

    s += "\&";这样直接报错
      

  5.   

    谢谢版主解答
    可是程序运行lable的text确实是aaa呢?
    不管转义的事,winform 下是吧,应该不会出现这种问题
    嗯,vs2010 winform 代码如下:        private void button1_Click(object sender, EventArgs e)
            {
                string s = "aaa";
                s += "&";
                label1.Text = s;
            }运行结果:
      

  6.   

    我知道是不是你的label宽度不够
      

  7.   

    不是,label的autosize属性是true;
    按照你的思路,我加了一个textbox,结果如下:真是奇怪
      

  8.   

                string s = "aaa";
                s += @"&";
                label1.Text = s;
    加一个: @ 看看。
      

  9.   

    一样不显示。字符串s确实为"aaa&",应该是微软Label的问题,可能是个bug?
      

  10.   

    那个&是有特殊含义的,紧跟在其后面的字符会成为access key,字母下面会显示下划线,可以做类似快捷键那样的东西。你在后面再添加个字符看看。对于Vista,Win7这些系统,缺省那个下划线不显示的,按一下Alter键就显示出来了。
      

  11.   

    用二个 &s += "&&";
      

  12.   

    MSDN中的内容:
    A Label participates in the tab order of a form, but does not receive focus (the next control in the tab order receives focus). For example, if the UseMnemonic property is set to true, and a mnemonic character—the first character after an ampersand (&)—is specified in the Text property of the control, when a user presses ALT+ the mnemonic key, focus moves to the next control in the tab order.