Streamwriter/Textwriter中特定的字符串长度(作为txt文件) [英] specific string length in streamwriter/textwriter as txt file
问题描述
我的数据行检索一个字符串长度不知道,我的代码是:
my data row retrieve one row string length don''t know,my code is :
string s="";
//s store retrieve data value.
streamwriter sw;
sw.writeline(s.substring(0,s.length<=30))
但这是错误的.
我想根据数据检索显示30个字符或少于30个字符,但我想为打印dmp打印机dos(例如销售账单或发票打印)写30个字符或少于30个字符.任何人都可以帮助我,我新学习了C#.
如果我的检索字符串长度为20,则余下的10个字符显示为Blanck.例如mystring + 10个字符是Blanck = 30个字符长的字符串.它用于数据表行格式
下面的代码不起作用
but it''s error.
I want show the character 30 or less than 30 as per data retrieve but I want to write 30 or less than 30 character for print dmp printer dos like,sales bill or invoice print.any one help me, I new learn c#.
if my retrieve string length is 20 the balance 10 character show Blanck. like mystring+10 character is Blanck = 30 character length string.It use in datatable rows format
below code not working
private string GetFormatedText(string Cont,int Length)
{
int rLoc=Length-Cont.Length;
if(rLoc<0)
{
Cont =Cont.Substring(0,Length);
}
else
{
int nos;
for(nos=0;nos Cont =Cont +" ";
}
return(Cont);
}
推荐答案
解决方案非常简单:从一开始就开始阅读基本语言手册.您真的需要它.
看起来您对语法不太了解,可能对参数,对象等也不太了解.首先,语法区分大小写.必须为s.Substring(0, 30)
.像s.Length<=30
这样的东西是布尔表达式.您只能在需要布尔参数的地方使用它;也应该在if
,while
等中使用.
http://msdn.microsoft.com/en-us/library/aka44szs.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/kx37x362.aspx [ ^ ].
—SA
The solution is very simple: start reading elementary language manual from the very beginning. You really needs it.
It looks like you have a very little idea of the syntax, and probably about parameters, objects, etc. First of all, the syntax is case-sensitive. Must bes.Substring(0, 30)
. A thing likes.Length<=30
is a Boolean expression; you can only use it where Boolean parameter is expected; it is also expected inif
,while
, etc.
http://msdn.microsoft.com/en-us/library/aka44szs.aspx[^],
http://msdn.microsoft.com/en-us/library/kx37x362.aspx[^].
—SA
如果我理解正确,您最多希望显示30个字符.
为此,您需要进行一些小的更改
If I understand correctly, you want to show up to a maximum of 30 characters.
To do that you have to make a small change
if(s.length <= 30)
sw.writeline(s);
else
sw.writeline(s.substring(0,30));
这篇关于Streamwriter/Textwriter中特定的字符串长度(作为txt文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!