c#中连字符的非破坏字符是什么?

时间:2016-06-01 13:09:38

标签: c# wpf character hyphen

我用" \ u00A0"将空格替换为非空格。 我需要相同的连字符(破折号)。什么是c#中最好的解决方案?

编辑:我试图添加建议的字符,但"?"出现而不是" - "。在调试中,我可以看到" - "在转换期间,但最终它的"?"。

我在其他帖子中发现" - "这不是一个真正的破折号可能导致"?"而不是破折号! (Post regarding "-" and "?")我需要一个解决方案,因为我需要一个不间断的破折号......

public void SetText(string text)
    {
        if (Mode == ControlMode.DesignTime)
        {
            string textToUse = string.Empty;
            string offlineScreenText = text;
            if (offlineScreenText != null)
            {
                int lblLen = Math.Min(Convert.ToInt32(_settings.MultilineLength), offlineScreenText.Length);
                textToUse = lblLen > 0 ? offlineScreenText.Substring(0, lblLen) : offlineScreenText;
            }

            _textBox.Text = textToUse;
        }
        else
        {
            if (!VerifyNumericValidity(text))
            {
                return;
            }

            _lastValue = text;

            // if there's a length limit, apply it to _lastValue 
            ApplyLengthLimit();

            if (text.Length > _textBox.MaxLength)
            {
                text = text.Substring(0, _textBox.MaxLength);
            }

            // Convertion to whitespaces in order to ignore the difference 
            // between non-breakable space ("\u00A0") and whitespaces (" ")
            string whitespacesText = text.Replace(" ", "\u00A0");
            whitespacesText = text.Replace("-", "\u2011");
            if (!whitespacesText.Equals(_textBox.Text) && !whitespacesText.Equals(_textBox.Text.TrimEnd()))
            {
                _textBox.Text = text;
            }
        }            
    }

void _textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        int origCursorLocation = _textBox.SelectionStart;

        // Simple whitespeces aren't being wrapped normally, 
        // so we'll replace them with non-breakable spaces
        _textBox.Text = _textBox.Text.Replace(" ", "\u00A0");
        _textBox.Text = _textBox.Text.Replace("-", "\u2011");

        BindingLayer.RaisePresentationChanged(DependencyPropertyType.Text, _textBox.Text);

        _textBox.SelectionStart = origCursorLocation;
    }

1 个答案:

答案 0 :(得分:4)

这应该是你需要的连字符

"\u2011"

此处有一个特殊字符列表:http://wiki.mobileread.com/wiki/Special_characters