【问题标题】:Fit text into a fixed-width column after pressing a button按下按钮后将文本放入固定宽度的列
【发布时间】:2019-02-09 15:27:39
【问题描述】:

我在文本框中有文本,我想将其格式化为 30 到 60 个字符的固定宽度列。允许用户指定每行的宽度(即列的宽度),但默认为 50 个字符。

我已经做了一些研究,但是我找不到任何关于我的目标是什么。 我试过String.Format 但是,它什么也没做

string test = ""
        if(txtBoxContent.Text == string.Empty)
        {
            MessageBox.Show("There are no '>' symbols to remove. Please paste the infected text first");
        }

        else if (!txtBoxContent.Text.Contains(">"))
        {
            MessageBox.Show("There are no '>' symbol(s) in the text OR are already removed");
        }

        else
        {
            contentWithoutCharacters = txtBoxContent.Text.Replace(">", "");
            txtBoxContent.Text = contentWithoutCharacters;


            //MessageBox.Show("Removed Successfully");

            test = string.Format("{0,50}", contentWithoutCharacters); // this
        }

    }

我假设我采取了错误的方法或没有正确使用string.format。任何指导将不胜感激

更新: 我想我没有清楚地解释我自己。

例如:以下文字为原文

我在文本框中有文本,我想将其格式化为 30 到 60 个字符的固定宽度列。允许用户指定每行的宽度(即列的宽度),但默认为 50 个字符。

当我按下按钮时,我希望上面的文本每行显示 50 个字符

我想将文本框中的文本格式化为固定宽度的换行 30 到 60 个字符的列。允许用户指定每行的换行宽度(即列的宽度),但默认为 50 个字符。

希望现在更有意义

【问题讨论】:

  • 我似乎不明白你的问题,但string.PadRight(int) 是你要找的吗?
  • 您的文本框是否使用了固定宽度的字体?在可变宽度字体中,每个字母都有不同的宽度,并且空格小于 w
  • 那个 sn-p 太不合适了,以至于很难猜出你想要完成什么。随机:文本适合多少取决于字体大小,而不是字符数,使用 TextRenderer.MeasureText()。
  • @不工作的人。我的意思是每行应该有 50 个字符的默认值,除非用户自己更改数字
  • @Steve 我不知道。我怎样才能找到它?

标签: c# desktop-application


【解决方案1】:

如果您决定将字符串分解为 n 宽度的片段,这里有一个方法。但是,您很快就会发现字符串以您不希望的方式分解……这就是为什么您让 cmets 想知道您为什么要这样做。

  var originalText = "Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! Now is the time; yada yada; thing and the thing! ";

  txtBoxContent.Clear();
  txtBoxContent.WordWrap = false; //--> to make sure nothing spills to the next line
  var part = string.Empty;

  var index = 0; //--> define this outside the loop, so you can use it to pick up any remainder
  var width = 30;  //--> ...or whatever width you need

  for ( index = 0; index < ( originalText.Length - width ); index += width )
  {
    part = originalText.Substring( index, width );
    //--> do something useful with the fixed-length part...
    txtBoxContent.Append( part + Environment.NewLine );
  }

  //--> deal with the remainder, if any...
  if ( index < originalText.Length )
  {
    //--> the last piece...it will be less than width wide...
    part = originalText.Substring( index, originalText.Length - index );
    txtBoxContent.Append( part );
  }

...这就是你得到的:

Now is the time; yada yada; th
ing and the thing! Now is the
time; yada yada; thing and the
 thing! Now is the time; yada
yada; thing and the thing! Now
 is the time; yada yada; thing
 and the thing! Now is the tim
e; yada yada; thing and the th
ing! Now is the time; yada yad
a; thing and the thing! Now is
 the time; yada yada; thing an
d the thing!

【讨论】:

  • 非常感谢!它只有一个问题。如果他们说宽度应该是 50 个字符,我应该输入 49(在计算中它从 0 开始)?
  • 不,宽度变量应该是他们所说的......SubString 将宽度作为第二个参数,循环将进行数学运算。 唯一你需要改变的是width的值。
  • 另请注意...如果您希望显示 n 个字符,则不计算 Environment.NewLine...可能是 1 个字符或 2 个字符...但无论哪种方式,它都不会影响显示字符的宽度。
  • 感谢您的清晰解释。最后一个问题,如果我需要minWIdth =30maxwidth=60,这段代码可以工作吗?
【解决方案2】:

您可以根据需要使用 padleft 或 padright 方法。下面是padright方法的例子:

string str  = "HelloWorld"
str.PadRight(11);

输出 = 'HelloWorld'

下面是padleft方法的例子:

string str  = "HelloWorld"
str.PadLeft(11);

输出 = 'HelloWorld'

【讨论】:

  • padleft 消耗所有字符,然后将剩余数量的字符作为空格保留在左侧,类似地,padright 消耗所有字符,然后将剩余数量的字符作为空格保留在右侧。
  • 它似乎没有任何影响
  • 您应该根据需要使用 str.PadRight(number) 中的数字...假设您输入了一个大小为 5 的字符串“Hello”,但您希望它应该被视为 50 所以使用这条线 str.PadRight(50);它会在 "Hello" 之后添加 45 个空格,并认为它是一个长度为 50 的字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
  • 2010-10-26
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多