Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my data row retrieve one row string length don't know,my code is :
C#
string s="";
//s store retrieve data value.
streamwriter sw;
sw.writeline(s.substring(0,s.length<=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
C#
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);
}
Posted
Updated 26-Jan-12 21:31pm
v7

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 be s.Substring(0, 30). A thing like s.Length<=30 is a Boolean expression; you can only use it where Boolean parameter is expected; it is also expected in if, while, etc.

http://msdn.microsoft.com/en-us/library/aka44szs.aspx[^],
http://msdn.microsoft.com/en-us/library/kx37x362.aspx[^].

—SA
 
Share this answer
 
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
C#
if(s.length <= 30)
   sw.writeline(s);
else
   sw.writeline(s.substring(0,30));
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jan-12 12:25pm    
That is correct and would work, but it will not compile due to wrong casing.

Besides, just the last line will work exactly as whole code. In there is not enough characters to return all 30, Substring will return as many as possible. That's why I've shown only s.Substring :-).

So, I voted 4.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900