空格、文本框中断(特定条件)
我需要在距 texbox 第 30 个字符最近的空间处进行中断,对此我得到了很好的答案:
var x = 30;
if (textBox1.Text.Length > x)
{
var index = textBox1.Text.Select((c, i) => new {c, i}).TakeWhile(q => q.i < x).Where(q => q.c == ' ' ).Select(q => q.i).Last();
textBox1.Text = textBox1.Text.Insert(index, Environment.NewLine);
}
唯一的问题是我需要排除“@A”、“@B”等字符,因为它们用于文本格式化。
I need to break on closest space to 30th character of texbox, and I got very good answer for that:
var x = 30;
if (textBox1.Text.Length > x)
{
var index = textBox1.Text.Select((c, i) => new {c, i}).TakeWhile(q => q.i < x).Where(q => q.c == ' ' ).Select(q => q.i).Last();
textBox1.Text = textBox1.Text.Insert(index, Environment.NewLine);
}
Only problem is that I need to exclude from counting characters like "@A", "@B", because they are used for text formatting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管可能不是最干净的解决方案。如果您只是依靠 @(或执行正则表达式来检测模式)并将该数字添加到 x (30),例如:
编辑
为了确保您的计数仅计算前 30 个字符(不包括 ' @'),可以提前进行聚合:
Although perhaps not the cleanest solution. If you simply count on @ (or perform a regex to detect patterns) and add that number to x (30) like:
edit
In order to make sure your count only counts the first 30 characters (excluding '@'), you can perform an aggregate in advance:
您可以尝试下面的代码。
You can try the code below.