空格、文本框中断(特定条件)

发布于 2024-12-17 08:17:26 字数 391 浏览 1 评论 0原文

我需要在距 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 技术交流群。

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

发布评论

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

评论(4

那请放手 2024-12-24 08:17:26

尽管可能不是最干净的解决方案。如果您只是依靠 @(或执行正则表达式来检测模式)并将该数字添加到 x (30),例如:

            int paramCount = test.Where(c => c == '@').Count();

            var index = test.Select((c, i) => new { c, i })
                            .TakeWhile(q => q.i < x + paramCount)
                            .Where(q => q.c == ' ')
                            .Select(q => q.i)
                            .Last();

编辑

为了确保您的计数仅计算前 30 个字符(不包括 ' @'),可以提前进行聚合:

            int paramCount = test.Select((c, i) => new { c, i })
                                 .Aggregate(0, (count, s) => s.c == '@' && s.i < x + count ? count + 1 : count);

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:

            int paramCount = test.Where(c => c == '@').Count();

            var index = test.Select((c, i) => new { c, i })
                            .TakeWhile(q => q.i < x + paramCount)
                            .Where(q => q.c == ' ')
                            .Select(q => q.i)
                            .Last();

edit

In order to make sure your count only counts the first 30 characters (excluding '@'), you can perform an aggregate in advance:

            int paramCount = test.Select((c, i) => new { c, i })
                                 .Aggregate(0, (count, s) => s.c == '@' && s.i < x + count ? count + 1 : count);
鱼忆七猫命九 2024-12-24 08:17:26
textBox1.Text.Replace("@A", "").Replace("@B", "")...
textBox1.Text.Replace("@A", "").Replace("@B", "")...
树深时见影 2024-12-24 08:17:26

您可以尝试下面的代码。

string sTemp = textBox1.Text.Substring(0, 30);
sTemp = sTemp.Replace(" @A ", "");
sTemp = sTemp.Replace("@A ", "");
sTemp = sTemp.Replace(" @A", "");
sTemp = sTemp.Replace("@A", "");

sTemp = sTemp.Replace(" @B ", "");
sTemp = sTemp.Replace("@B ", "");
sTemp = sTemp.Replace(" @B", "");
sTemp = sTemp.Replace("@B", "");

int numberOfLeak = 30 - sTemp.Length;
var x = 30 + numberOfLeak;
if (textBox1.Text.Length > x)
{
    textBox1.Text = textBox1.Text.Insert(x, Environment.NewLine);
} 

You can try the code below.

string sTemp = textBox1.Text.Substring(0, 30);
sTemp = sTemp.Replace(" @A ", "");
sTemp = sTemp.Replace("@A ", "");
sTemp = sTemp.Replace(" @A", "");
sTemp = sTemp.Replace("@A", "");

sTemp = sTemp.Replace(" @B ", "");
sTemp = sTemp.Replace("@B ", "");
sTemp = sTemp.Replace(" @B", "");
sTemp = sTemp.Replace("@B", "");

int numberOfLeak = 30 - sTemp.Length;
var x = 30 + numberOfLeak;
if (textBox1.Text.Length > x)
{
    textBox1.Text = textBox1.Text.Insert(x, Environment.NewLine);
} 
客…行舟 2024-12-24 08:17:26
        string oriText = textBox1.Text;//Original text that you input
        int charPerLine = 30;//Number of character per line
        string sKeyword = "@A|@B";//You can add more template here, the separator is "|"
        string[] arrKeyword = sKeyword.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        ArrayList arrListKeyword = new ArrayList();
        for (int i = 0; i < arrKeyword.Length; i++)
        {
            arrListKeyword.Add(" " + arrKeyword[i] + " ");
            arrListKeyword.Add(arrKeyword[i] + " ");
            arrListKeyword.Add(" " + arrKeyword[i]);
            arrListKeyword.Add(arrKeyword[i]);
        }
        int nextIndex = 0;
        while (true)
        {
            //Check if the sub string after the NewLine has enough length
            if (charPerLine < oriText.Substring(nextIndex).Length)
            {
                string sSubString = oriText.Substring(nextIndex, charPerLine);
                //Replace all keywords with the blank
                for (int i = 0; i < arrListKeyword.Count; i++)
                {
                    sSubString = sSubString.Replace(arrListKeyword[i].ToString(), "");
                }

                int numberOfLeak = charPerLine - sSubString.Length;
                int newLineIndex = nextIndex + charPerLine + numberOfLeak;//find the index to insert NewLine

                oriText = oriText.Insert(newLineIndex, Environment.NewLine);//Insert NewLine
                nextIndex = newLineIndex + Environment.NewLine.Length;//Environment.NewLine cost 2 in length

            }
            else
            {
                break;
            }
        }

        textBox1.Text = oriText;
        string oriText = textBox1.Text;//Original text that you input
        int charPerLine = 30;//Number of character per line
        string sKeyword = "@A|@B";//You can add more template here, the separator is "|"
        string[] arrKeyword = sKeyword.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        ArrayList arrListKeyword = new ArrayList();
        for (int i = 0; i < arrKeyword.Length; i++)
        {
            arrListKeyword.Add(" " + arrKeyword[i] + " ");
            arrListKeyword.Add(arrKeyword[i] + " ");
            arrListKeyword.Add(" " + arrKeyword[i]);
            arrListKeyword.Add(arrKeyword[i]);
        }
        int nextIndex = 0;
        while (true)
        {
            //Check if the sub string after the NewLine has enough length
            if (charPerLine < oriText.Substring(nextIndex).Length)
            {
                string sSubString = oriText.Substring(nextIndex, charPerLine);
                //Replace all keywords with the blank
                for (int i = 0; i < arrListKeyword.Count; i++)
                {
                    sSubString = sSubString.Replace(arrListKeyword[i].ToString(), "");
                }

                int numberOfLeak = charPerLine - sSubString.Length;
                int newLineIndex = nextIndex + charPerLine + numberOfLeak;//find the index to insert NewLine

                oriText = oriText.Insert(newLineIndex, Environment.NewLine);//Insert NewLine
                nextIndex = newLineIndex + Environment.NewLine.Length;//Environment.NewLine cost 2 in length

            }
            else
            {
                break;
            }
        }

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