Use resouce string as format string


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Use resouce string as format string

  1. #1
    David Chu Guest

    Use resouce string as format string


    I have the following codes:

    ResourceManager rm;
    String sF;
    ...
    // Use resource string as format
    sF = rm.GetString("101"); // "Name: {0}\tWeight: {1}"
    Console.WriteLine( sF ); // Name: {0}\tWeight: {1}
    Console.WriteLine( sF, "Peter", 180 ); // Name: Peter\tWeight: 180
    // Use string variable as format
    sF = "Name: {0}\tWeight: {1}";
    Console.WriteLine( sF ); // Name: {0} Weight: {1}
    Console.WriteLine( sF, "Peter", 180 ); // Name: Peter Weight: 180

    The problem is that \t in format string from resource does not work the
    same way as in a string variable or constant (as expected as a tab).

    Why?

    D. Chu

  2. #2
    Rune Bivrin Guest

    Re: Use resouce string as format string

    When you enter \t in the editor, the compiler replaces that with the
    actual tab character in the string. When the resource string contains \t,
    it simply contains the two characters '\' and 't'. I don't know how to
    get a tab into the resource, though. I haven't looked at resources yet.
    perhaps?

    --
    Rune Bivrin
    - OOP since 1989
    - SQL Server since 1990
    - VB since 1991

    "David Chu" <chud@hotmail.com> wrote in news:3e1c7714$2
    @tnews.web.devx.com:

    >
    > I have the following codes:
    >
    > ResourceManager rm;
    > String sF;
    > ...
    > // Use resource string as format
    > sF = rm.GetString("101"); // "Name: {0}\tWeight: {1}"
    > Console.WriteLine( sF ); // Name: {0}\tWeight: {1}
    > Console.WriteLine( sF, "Peter", 180 ); // Name: Peter\tWeight: 180
    > // Use string variable as format
    > sF = "Name: {0}\tWeight: {1}";
    > Console.WriteLine( sF ); // Name: {0} Weight: {1}
    > Console.WriteLine( sF, "Peter", 180 ); // Name: Peter Weight: 180
    >
    > The problem is that \t in format string from resource does not work the
    > same way as in a string variable or constant (as expected as a tab).
    >
    > Why?
    >
    > D. Chu
    >



  3. #3
    David Chu Guest

    Re: Use resouce string as format string


    Rune Bivrin,

    You are right. '\t' in a string in source code is interpreted to a tab code,
    however, '\t' in a string retrieved from resouce is '\' and 't'. If you compare
    the lenghth of two string, you can find it out.

    I remembered that in VB6.0 the newline code has to be none-ascii code '\0'
    in a string and it cannot be added directly to resouce file by VB's resouce
    editor. What I did is by using other editor to paste the none-ascii code
    to the resouce file then to compile it. Maybe it is the same case for C#'s
    resouce file (none-ascii codes like tab, newline have to be hard-coded to
    resouce file through other editors).

    D. Chu

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center