获取用户输入期间的最后 3 个字符

发布于 2024-10-11 11:02:12 字数 1553 浏览 3 评论 0原文

当用户在 richTextbox 中写入一些文本时,我需要从 richTextBox 获取最后三个字符。

我将属性绑定到扩展 WPF 工具包的 richTextBox 的 Text 属性上。

public string RtbText
{
    get { return _rtbText; }
    set
    {
        _rtbText = value;
        NotifyPropertyChanged("RtbText");
    }
}

我使用 Reactive Extensions for .NET (Rx) 并在属性 RtbText 上创建 Observer

    Observable.FromEvent<PropertyChangedEventArgs>(this, "PropertyChanged")
        .Where(e => e.EventArgs.PropertyName == "RtbText")
        .Select(_ => this.RtbText)
        .Where(text => text.Length > 1)
        .Do(AddSmiles)
        .Throttle(TimeSpan.FromSeconds(1))
        .Subscribe(GetLastThreeChars);

   private void GetLastThreeChars(string text)
   {
       if (text.Length > 3)
       {
           string lastThreeChars = text.Substring(text.Length - 2, text.Length);
       }
   }

但如果我开始在 richTextBox 中输入内容,则会出现此异常:

索引和长度必须引用字符串中的位置。
参数名称:长度

at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
在 System.String.Substring(Int32 startIndex, Int32 length)
在 C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\IoC.Get\Pokec_Messenger\ver.beta\Pokec_Messenger\ 中的 WpfApplication1.MainWindow.GetLastThreeChars(String text) WpfApplication1\MainWindow.xaml.cs:第 97 行
在 System.Linq.Observable.<>c
_DisplayClass389`1.<>c_DisplayClass38b.b_388(TSource x)

I need get last three character from richTextBox during when the user writes some text in richTextbox.

I bind property on Text property of richTextBox from Extended WPF toolkit.

public string RtbText
{
    get { return _rtbText; }
    set
    {
        _rtbText = value;
        NotifyPropertyChanged("RtbText");
    }
}

I use Reactive Extensions for .NET (Rx) and make Observer on property RtbText

    Observable.FromEvent<PropertyChangedEventArgs>(this, "PropertyChanged")
        .Where(e => e.EventArgs.PropertyName == "RtbText")
        .Select(_ => this.RtbText)
        .Where(text => text.Length > 1)
        .Do(AddSmiles)
        .Throttle(TimeSpan.FromSeconds(1))
        .Subscribe(GetLastThreeChars);

   private void GetLastThreeChars(string text)
   {
       if (text.Length > 3)
       {
           string lastThreeChars = text.Substring(text.Length - 2, text.Length);
       }
   }

But if I start typing in richTextBox I get this exception:

Index and length must refer to a location within the string.
Parameter name: length

at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at WpfApplication1.MainWindow.GetLastThreeChars(String text) in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\IoC.Get\Pokec_Messenger\ver.beta\Pokec_Messenger\WpfApplication1\MainWindow.xaml.cs:line 97
at System.Linq.Observable.<>c
_DisplayClass389`1.<>c_DisplayClass38b.b_388(TSource x)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

汐鸠 2024-10-18 11:02:12

如果 text.Length > 3 (假设它是 4),那么:

text.Length - 2 = 2

所以你的代码是:

string lastThreeChars = text.Substring(2, 4);

这将会失败,因为你要求子字符串中有四个字符,这使它超出了范围。

String.Substring 方法(Int32、Int32)

从此实例中检索子字符串。子字符串从指定的字符位置开始,并具有指定的长度

此外,您的测试和起始位置不正确。不要忘记 C# 数组和字符串是零索引的。检查长度严格大于 3 的情况,当您想要返回整个字符串时,您会错过用户恰好输入了三个字符的情况。

您的代码需要是:

if (text.Length > 2)
{
    string lastThreeChars = text.Substring(text.Length - 3, 3);
}

如果事实上您不需要指定长度:

if (text.Length > 2)
{
    string lastThreeChars = text.Substring(text.Length - 3);
}

将返回字符串中的最后三个字符。

If text.Length > 3 (say it's 4) then:

text.Length - 2 = 2

So you're code is:

string lastThreeChars = text.Substring(2, 4);

This will fail as you are asking for four characters in the substring, which puts it out of range.

String.Substring Method (Int32, Int32)

Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

Additionally your test and starting position are incorrect. Don't forget that C# arrays and strings are zero indexed. Checking for the case of length strictly greater than 3 you'll miss the case when the user has entered exactly three characters when you want to return the entire string.

Your code needs to be:

if (text.Length > 2)
{
    string lastThreeChars = text.Substring(text.Length - 3, 3);
}

If fact you don't need to specify the length:

if (text.Length > 2)
{
    string lastThreeChars = text.Substring(text.Length - 3);
}

will return the last three characters in the string.

月下凄凉 2024-10-18 11:02:12

这是另一种形式。它获取从某个开始位置到结束的所有字符,

string lastThreeChars = text.Substring(text.Length - 3);

可能是 text.Length - 2。未经测试

This is another form. Which gets all the characters from some start position up to the end

string lastThreeChars = text.Substring(text.Length - 3);

maybe text.Length - 2. Untested

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文