打破空间,文本框(特定条件)

时间:2011-11-23 02:22:14

标签: c# asp.net line-breaks

我需要打破最近的空间到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”之类的字符,因为它们用于文本格式化。

4 个答案:

答案 0 :(得分:0)

textBox1.Text.Replace("@A", "").Replace("@B", "")...

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您可以尝试以下代码。

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

答案 3 :(得分:0)

        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;