none
string placement RRS feed

  • Question

  • I want to place to strings of variable lengths in a string for output.

    at position 5 for i1st string.

    at position 20 for 2nd string.

    Is there a way to do other than counting length of !st.

      thank you

    Tuesday, December 12, 2017 9:56 PM

Answers

  • Also try this:

    string first = "Text1";
    string second = "Text2";
    
    string output = ( string.Empty.PadRight( 4 ) + first ).PadRight( 19 ) + second;
    

    and this:

    string output = string.Format( "{0,4}{1,-15}{2}", string.Empty, first, second );

    If the first string is too long, it is not overwritten.

    • Proposed as answer by Fei HuModerator Thursday, December 14, 2017 7:18 AM
    • Marked as answer by Jordanhal Monday, December 18, 2017 8:24 PM
    Wednesday, December 13, 2017 7:15 AM

All replies

  • My guess is there is more to this than presented in your post, if so please elaborate e.g. how are you planning to create these variable length strings and what is the purpose of this task.

    Generally speaking, more details will get more responses while vague/less details were got nothing or little plus there will be questions such as mine above.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, December 12, 2017 11:16 PM
    Moderator
  • Hello Jordanhal,

    If you want to insert a string at some position, try to use String.Insert method.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Cor Ligthert Wednesday, December 13, 2017 12:27 PM
    Wednesday, December 13, 2017 3:00 AM
    Moderator
  • Also try this:

    string first = "Text1";
    string second = "Text2";
    
    string output = ( string.Empty.PadRight( 4 ) + first ).PadRight( 19 ) + second;
    

    and this:

    string output = string.Format( "{0,4}{1,-15}{2}", string.Empty, first, second );

    If the first string is too long, it is not overwritten.

    • Proposed as answer by Fei HuModerator Thursday, December 14, 2017 7:18 AM
    • Marked as answer by Jordanhal Monday, December 18, 2017 8:24 PM
    Wednesday, December 13, 2017 7:15 AM