获取用户输入期间的最后 3 个字符
当用户在 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: lengthat 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
text.Length > 3
(假设它是 4),那么:所以你的代码是:
这将会失败,因为你要求子字符串中有四个字符,这使它超出了范围。
String.Substring 方法(Int32、Int32)
此外,您的测试和起始位置不正确。不要忘记 C# 数组和字符串是零索引的。检查长度严格大于 3 的情况,当您想要返回整个字符串时,您会错过用户恰好输入了三个字符的情况。
您的代码需要是:
如果事实上您不需要指定长度:
将返回字符串中的最后三个字符。
If
text.Length > 3
(say it's 4) then:So you're code is:
This will fail as you are asking for four characters in the substring, which puts it out of range.
String.Substring Method (Int32, Int32)
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 fact you don't need to specify the length:
will return the last three characters in the string.
这是另一种形式。它获取从某个开始位置到结束的所有字符,
可能是 text.Length - 2。未经测试
This is another form. Which gets all the characters from some start position up to the end
maybe text.Length - 2. Untested