我想在文本框中添加一个空格(),直到最大长度达到1000。
我想写文本在文本框中的空格,所以文字是第一次,在文本完成后,空格开始,直到达到最大的文本框长度,例如。
,如下所示:
textbox1.text = "mytext" + " ";但我想要的空间,只是填补文本框,直到最大长度(1000)达到。
我想要的另一件事是,如果文本框中的文本大于最大长度,那么删除多余的文本(1000之后的文本)。
请帮帮忙
发布于 2014-01-23 13:46:32
您可以使用string.PadRight()方法。
textbox1.Text = textbox1.Text.PadRight(textbox1.MaxLength, ' ');发布于 2014-01-23 13:46:56
首先检查长度是否大于允许的最大长度,然后使用Substring将其缩减到大小。如果长度小于最大值,那么可以使用PadRight来填充文本.
string text = textbox1.Text;//get the text to manipulate
int max = 1000;
if(text.Length > max)//If the current text length is greater than max
text = text.Substring(0, max);//trim the text to the maximum allowed
else
text = text.PadRight(max, ' ');//pad extra spaces up until the correct length
//text will now be the same length as max (with spaces if required)
textbox1.Text = text;//set the new text value back to the TextBox注意到:由于您已经询问了如何将文本裁剪到最大长度,所以我假设您没有使用TextBox的MaxLength属性--因为这样已经可以防止添加超出限制的内容,所以我建议您使用这个选项,而不必担心自己是否需要修剪,您可以这样做:
textbox1.Text = textbox1.Text.PadRight(textbox1.MaxLength, ' ');发布于 2014-01-23 13:50:54
我想这就是你想要的,对吧?
public String writeMax(String myText, int maxLenght)
{
int count = myText.Length;
String temp = "";
if(count >= maxLength)
{
temp = myText.substring(0, maxLength);
}
else
{
for(int i = 0; i < maxLength - count; i++)
{
temp += " ";
}
temp = myText + temp;
}
return temp;
}https://stackoverflow.com/questions/21309880
复制相似问题