locked
String.Format: join two value in one padding RRS feed

  • Question

  • How can I do something like

    string.Format("({{0}{1},10})", "ABC", "DEF) // (    ABCDEF)
    string.Format("({{0} {1},10})", "ABC", "DEF) // (   ABC DEF)

    Example

    I have to solve tihs operation with format. And I do not have a chance to combine elsewhere.

    So is not it possible to do something like.

    Console.WriteLine(string.Format("({0,10})", "ABC" + " DEF"));

    I cant use join or concat or a + b methods

    Why i want only format pattern? Because of usage like this.

    //Dummy metod for example public static string applyFormat(String format, String values, Object object ) { member1 = reflect from object by named extracted string member2 = reflect from object by named extracted string or members[]= exploded string return String.Format(format, member1, member2); or return String.Format(format, members); }

    Usage Example

    Console.WriteLine(applyFormat("({ {0}{1},-10})", "Member1,Member2", object)); // "({ {0}{1},-10})": Reflect and Join Member1 and Member2 and apply padding joined value

    • Edited by Fatih BAHCECI Wednesday, February 12, 2014 7:32 AM More detail
    Tuesday, February 11, 2014 10:22 AM

Answers

  • Hello

    The below line is for Substituting the "value1" at {0} , "value2" at {1} and "value3" at {3} respectively

    string result = string.Format("{0}: {1} - {2}",  value1,   value2,   value3);

    Console.WriteLine(result);


    Tuesday, February 11, 2014 10:48 AM