在空格,文本框(特定条件)上断开



我需要在textbox的第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"这样的字符,因为它们用于文本格式化。

虽然可能不是最干净的解决方案。如果您只是依靠@(或执行正则表达式来检测模式)并将该数字添加到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);
textBox1.Text.Replace("@A", "").Replace("@B", "")...

您可以尝试下面的代码。

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);
} 
        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;

最新更新