none
source code RRS feed

  • Question

  • I have a 63 character string line that contains 2 other string abc and String xyz i want to print my second string xyz after first string contain 54 characters,if it does not have 54 character provide spacing?

    Example:

    String abc=" ";    // First 54 characters

    String xyz=" ";    // last 9 characters

    String line = abc + xyz ;

    Tuesday, April 9, 2013 5:15 AM

Answers

  • If you really can't work it out for yourself then a good way to research would be to go to your local supermarket and purchase a couple of items.  Study the receipt carefully.  In particular see if looking at the receipt and considering the very strong hints I have been giving you about proportional fonts gives you any idea of how to proceed.


    Paul Linton

    Wednesday, April 10, 2013 7:27 AM

All replies

  • Suggest me the methods for this functionality
    Tuesday, April 9, 2013 5:33 AM
  • Plz use:

    line.SubString(54);  //to fetch the last 9 characters.


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Tuesday, April 9, 2013 5:37 AM
  •  String line = abc.PadRight(54) + xyz;


    It all Happenz Sendil

    • Proposed as answer by PaulLinton Tuesday, April 9, 2013 6:23 AM
    • Unproposed as answer by anurag saraswat Wednesday, April 10, 2013 5:19 AM
    Tuesday, April 9, 2013 5:38 AM
  • thanks ...

    But basically my Requirement is ... i have a string item that has two string desc and string price like this  string item = " " + desc + " " + price ;   when i Print the item string, sometime price alignment from right side disturbed due to change in length of characters in desc, my total string length of item is 63 and string length of  desc is 53 , left 9 character for price string Suggest me the code.
    Tuesday, April 9, 2013 6:07 AM
  •  string item = string.Format(" {0,-54} {1,9}", desc, price);


    It all Happenz Sendil

    • Proposed as answer by PaulLinton Wednesday, April 10, 2013 4:27 AM
    • Unproposed as answer by anurag saraswat Wednesday, April 10, 2013 5:19 AM
    Tuesday, April 9, 2013 7:22 AM
  • This is my current output and i want to align my all item price in straight line above the subtotal

    Wednesday, April 10, 2013 4:22 AM
  • You have been given a couple of answers now.  Why do you keep restating the problem?  If you don't know how to implement the code you have been given then show us what you have done so far.  But, please, stop stating the problem over and over again.

    Paul Linton

    Wednesday, April 10, 2013 4:28 AM
  • But i use all the above solution for my string like string.format or padRight but no solution found.. 
    Wednesday, April 10, 2013 4:32 AM
  • You did it wrong.

    Show use the code.  It is absolutely, 100% impossible to fix your code if you don't show the code.


    Paul Linton

    Wednesday, April 10, 2013 4:33 AM
  •                 const string format = "{0,0} {1,4} {2,-5}";
                    int i = 0;
                    string quantity;
                    string size;
                    string desc;
                    string price;
                    string combo;

                    while (i < itemprint.Length)
                    {
                        string itemsline;
                        quantity = returnnile(itemprint[i].QUANTITY.ToString(), 2);
                        size = itemprint[i].DESCRIPTION.ToString();
                        
                        combo = size +"  "+ desc;
                        
                        price = string.Format("{0:0.00}", itemprint[i].PRICE).ToString();

                        itemsline = String.Format(format, quantity, combo, price) + Environment.NewLine;
                        
                        Printpart5 = Printpart5 + itemsline;
                        i = i + 1;
                    }
                    Printpart5 = Printpart5 + Dotline + Environment.NewLine;
                    string test = Printpart1 + Printpart2 + Environment.NewLine + Printpart3 + Environment.NewLine + Printpart4 + Printpart5 + Printpart6 + Environment.NewLine;

                    byte[] byteArray = Encoding.ASCII.GetBytes(test);
                    MemoryStream stream = new MemoryStream(byteArray);
                    reader = new StreamReader(stream);

                    PrintDocument pd = new PrintDocument();
                    pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
                    pd.Print();

                    pd.Dispose();

                    if (reader != null)
                        reader.Close();
                }
                catch (Exception ex)
                {
                    obj.ErrorLog(Convert.ToString(this), "callPrint", ex.Message.ToString(), String.Empty, false, Convert.ToChar("E"));
                }
            }
    Wednesday, April 10, 2013 4:47 AM
  • here is my code on which i m working

    Wednesday, April 10, 2013 4:54 AM
  • I can't see any evidence that you have tried to use the answers that you have been given.  You said that you tried to use the solutions given but it didn't work (hint, hint - those proposed as answers by other people are most likely to be right)

    Paul Linton

    Wednesday, April 10, 2013 4:55 AM
  •  int i = 0;
                    string quantity;
                    string size;
                    string desc;
                    string price;
                    string combo;

                    while (i < itemprint.Length)
                    {
                        string itemsline;
                        quantity = returnnile(itemprint[i].QUANTITY.ToString(), 2);
                        size = itemprint[i].DESCRIPTION.ToString();

                        if (size.Length == 1)
                        {
                            size = size.Replace(".", "N/A");
                        }
                        desc = itemprint[i].ORDER_DESCRIPTION.ToString();
                        
                        combo = size +"  "+ desc;
                        string temp = quantity+" "+ combo;
                        
                        price = string.Format("{0:0.00}", itemprint[i].PRICE).ToString();

                        itemsline = temp + price.PadRight(70) + Environment.NewLine;
                        
                        //itemsline = String.Format(format, quantity, combo, price) + Environment.NewLine;
                        
                        //itemsline = quantity + "  " + combo +" " + price + Environment.NewLine + itemprint[i].TOPPING_STRING.Replace("()", "") + Environment.NewLine;
                        
                        Printpart5 = Printpart5 + itemsline;
                        i = i + 1;
                    }

    Wednesday, April 10, 2013 5:13 AM
  • What do you think that price.PadRight(70) is going to do?  This was never suggested to you, but it is a learning exercise. 

    Use MSDN to find out if you can't guess (you have all the clues in front of you). 

    Are there other changes apart from price.PadRight(70)?


    Paul Linton

    Wednesday, April 10, 2013 5:23 AM
  • thanks..
    Wednesday, April 10, 2013 5:26 AM
  • Hi anurag saraswat

    If ur problem is solved, plz mark answers.

    Otherwises plz tell us by offering an example:

    1) Ur original string.

    2) What do u expect to get?


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Wednesday, April 10, 2013 5:33 AM
  • my String Print output  like this

    1 VG Tacos Ind  VG Tacos Ind  89.00

    1 Pasta Vg  IT WHPas Vg  95.00

    1 NVG Tacos Ind   NVG TacoInd 99.00

    But i want if Item Name character is increase or decrease, price place is fixed in right hand side

    i want like this

    1 VG Tacos Ind  VG Tacos Ind       89.00

    1 Pasta Vg  IT WHPas Vg              95.00

    1 NVG Tacos Ind   NVG TacoInd    99.00

    Wednesday, April 10, 2013 5:45 AM
  • my string is like this   

    string itemsline = quantity + "  " + Item Name +" " + price + Environment.NewLine;

    but problem is when  Item Name character is increasing then my price position is disturbed


    Wednesday, April 10, 2013 5:55 AM
  • So, you need to control the length of Item.Name.  How are you going to do that?  The answer has been given many, many times in this thread.

    Paul Linton

    Wednesday, April 10, 2013 5:58 AM
  • But no above method is properly work on my Print Bill ,when i am using string.format in console application it work perfectly but when i use string.format in my window application for Printing a Bill it's not working properly.
    Wednesday, April 10, 2013 6:07 AM
  • Show us the code

    Paul Linton

    Wednesday, April 10, 2013 6:08 AM
  •                 int i = 0;
                    string quantity;
                    string size;
                    string desc;
                    string price;
                    string combo;

                    while (i < itemprint.Length)
                    {
                        string itemsline;
                        quantity = returnnile(itemprint[i].QUANTITY.ToString(), 2);
                        size = itemprint[i].DESCRIPTION.ToString();
                        desc = itemprint[i].ORDER_DESCRIPTION.ToString();

                        combo = size +"  "+ desc;

                        price = string.Format("{0:0.00}", itemprint[i].PRICE).ToString();

                       itemsline = String.Format({0,1} {1,4} {2,-5}, quantity, combo, price) +      Environment.NewLine;
                       

    Printpart5 = Printpart5 + itemsline;
                        i = i + 1;
                    }

                                                                                                                            
    Wednesday, April 10, 2013 6:14 AM
  • Can you describe what you think the line that starts

    itemsline = String

    would do.  In particular, what do you think the things inside the '{' and '}' do?  Just telling us may help you understand how to fix your problem.


    Paul Linton

    Wednesday, April 10, 2013 6:18 AM
  • i m using

    itemsline = String.Format({0,1} {1,4} {2,-5}, quantity, combo, price) +  Environment.NewLine;

    and thought after running this string itemline    "quantity, combo, price" position is fixed on slip.

                       
    Wednesday, April 10, 2013 6:26 AM
  • Can you describe what each of the numbers do?  Eg fill in the dots

    In {0,1} the zero means ... and the 1 means ...

    In {1,4} the one means ... and the 4 means ...

    In {2,-5} the two means ... and the -5 means ...


    Paul Linton

    Wednesday, April 10, 2013 6:28 AM
  • i m currently applying this {0,3} {1,-15} {2,-28}

    this mean {0,3} 0 mean quantity,quantity print on 3 character count from right side

    {1,-15} 1 mean combo,combo print on 15 character count from left side

    {2,-28} 2 mean price, price print on 28 character count from left side

    Wednesday, April 10, 2013 6:40 AM
  • Here is the solution...

    itemsline = String.Format({0,1} {1,-54} {2,9:0.00}, quantity, combo, price) + Environment.NewLine;



    It all Happenz Sendil

    • Proposed as answer by ThankfulHeart Wednesday, April 10, 2013 7:06 AM
    Wednesday, April 10, 2013 6:41 AM
  • Hi,

    But It's not working after applying this OUTPUT is like this

    1 N/A  Garlic Bread                                          70.00

    1 VG Tacos Ind  VG Tacos Ind                                 89.00

    1 N/A  Wing                                                 110.00

    Wednesday, April 10, 2013 6:53 AM
  • I've been waiting for you to get to this point.

    You are using a proportional font, so the characters are not all the same width.


    Paul Linton

    Wednesday, April 10, 2013 6:54 AM
  • ok..

    soo what approach i follow now..?

    Wednesday, April 10, 2013 6:57 AM
  • What ideas do you have?  Hint, the problem is because you are using a proportional font.


    Paul Linton

    Wednesday, April 10, 2013 6:59 AM
  • It works on me quite well in Console App:

    class Program
        {
            static void Main(string[] args)
            {
              Console.WriteLineString.Format("{0,1} {1,-54} {2,9:0.00}""1""N/A  Garlic Bread""70.00"+  Environment.NewLine);
              Console.WriteLine(String.Format("{0,1} {1,-54} {2,9:0.00}""1""VG Tacos Ind  VG Tacos Ind ""70.00"+ Environment.NewLine);
              Console.WriteLine(String.Format("{0,1} {1,-54} {2,9:0.00}""1""N/A  Wing""70.00"+ Environment.NewLine);
            }
        }


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Wednesday, April 10, 2013 7:07 AM
  • It works on me quite well in Console App:


    What you mean to say is "It works quite well with a non-proportionally spaced font" Console is irrelevant.

    Paul Linton

    Wednesday, April 10, 2013 7:10 AM
  • Temp=quantity + combo;

    String itemsline =  Temp + price  +  Environment.NewLine;

    i found the maximum length of itemline string it is 65, and Temp string max length is 56 , Now

    i think if i make a method and define String itemline maximum length=65 then check if Temp string = 56 then print Temp + Price but if Temp is not equal to 56 then it can check remaining character for 56 and add " " space to complete 56 character and then print Temp + Price.   

    Wednesday, April 10, 2013 7:10 AM
  • Yes you are right it work perfectly on console application.. but i work on window application where i want to print this string on Slip.
    Wednesday, April 10, 2013 7:12 AM
  • Hi anurag,

    Where did u apply these codes?

    In a winForm?

    Can u show us ur full codes?


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Wednesday, April 10, 2013 7:13 AM
  • Nope, that won't do it (and all you are doing is rewriting what string.Format does).

    Two strings of 64 characters will print with different widths when you use a proportional font.

    Try it, give one product a description of

    iiiiiiiiiiiiiiiiiii

    and another a description of

    XXXXXXXXXXXXXXXXXXX

    in both cases there are 20 characters.  The second one will always print wider than the first when you use a proportional font.


    Paul Linton

    Wednesday, April 10, 2013 7:15 AM
  • Console, WinForm, WebForm, WPF, Silverlight, Android, IOS, etc are all irrelevant.  It is the type of font which counts. 

    Paul Linton

    Wednesday, April 10, 2013 7:17 AM
  • I applying this code on my WPF Application for printing a Bill

    Wednesday, April 10, 2013 7:19 AM
  • In WPF Application

    Not important.  It is the font which matters.

    Paul Linton

    Wednesday, April 10, 2013 7:23 AM
  • soo what can i do now.. ? any idea.. for my problem
    Wednesday, April 10, 2013 7:24 AM
  • If you really can't work it out for yourself then a good way to research would be to go to your local supermarket and purchase a couple of items.  Study the receipt carefully.  In particular see if looking at the receipt and considering the very strong hints I have been giving you about proportional fonts gives you any idea of how to proceed.


    Paul Linton

    Wednesday, April 10, 2013 7:27 AM