String Formatting (Like in VB)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jmanion@forte-industries.com

    String Formatting (Like in VB)

    Hello,

    I have a problem, I'm hoping someone can help.

    I have a string, lets say: "111111"; // Not always numeric
    I have a format mask, lets say: "(00) 0000"; // Again, not always
    numeric.

    The mask and the value are passed into a method.

    private string GiveMeTheFormat tedString(strin g i_Value, string i_Mask)
    {

    }

    I want the output of this method to be: "(11) 1111"

    Remember, I can't assume that my input string or my mask will be
    numeric and the mask will be different.

    Help. :-)

    I've tried this:

    string retVal = String.Format(" {0:" + iMask + "}", i_Value);

    No luck, it always comes back as the original input.

    I even tried the above hard coding the format mask in, i.e. ("{0:(00)
    0000}",i_Value) ;

    still no luck.

    Thanks for the help, in advance.

    John

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: String Formatting (Like in VB)

    What is the function in VB that you use to do this? More than likely,
    it exists in the Microsoft.Visua lBasic namespace. You can add a reference
    to Microsoft.Visua lBasic.dll and use the method that you know will work
    (rather than write your own).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    <jmanion@fort e-industries.comw rote in message
    news:1159209381 .511882.278180@ e3g2000cwe.goog legroups.com...
    Hello,
    >
    I have a problem, I'm hoping someone can help.
    >
    I have a string, lets say: "111111"; // Not always numeric
    I have a format mask, lets say: "(00) 0000"; // Again, not always
    numeric.
    >
    The mask and the value are passed into a method.
    >
    private string GiveMeTheFormat tedString(strin g i_Value, string i_Mask)
    {
    >
    }
    >
    I want the output of this method to be: "(11) 1111"
    >
    Remember, I can't assume that my input string or my mask will be
    numeric and the mask will be different.
    >
    Help. :-)
    >
    I've tried this:
    >
    string retVal = String.Format(" {0:" + iMask + "}", i_Value);
    >
    No luck, it always comes back as the original input.
    >
    I even tried the above hard coding the format mask in, i.e. ("{0:(00)
    0000}",i_Value) ;
    >
    still no luck.
    >
    Thanks for the help, in advance.
    >
    John
    >

    Comment

    • jmanion@forte-industries.com

      #3
      Re: String Formatting (Like in VB)


      In VB, I'd use the Format function.

      As I bring code forward from the VB6 world, I'm supposed to work under
      the Mantra:

      VB = Bad;
      VC# = Good.

      :-)

      So, if I can do it, I'd like to not use the VB namespace.

      John

      Comment

      • jmanion@forte-industries.com

        #4
        Re: String Formatting (Like in VB)


        In VB, I'd use the Format function.

        As I bring code forward from the VB6 world, I'm supposed to work under
        the Mantra:

        VB = Bad;
        VC# = Good.

        :-)

        So, if I can do it, I'd like to not use the VB namespace.

        John

        Comment

        • Michael D. Ober

          #5
          Re: String Formatting (Like in VB)

          Bad mantra, especially if you don't have a C++ background. VB 2005 is just
          as expressive as VC#. Yes, there are some differences between the
          languages, but they are functionally 99+% identical and both are fully
          supported by MS.

          Mike Ober.


          <jmanion@fort e-industries.comw rote in message
          news:1159214425 .730478.63400@i 42g2000cwa.goog legroups.com...
          >
          In VB, I'd use the Format function.
          >
          As I bring code forward from the VB6 world, I'm supposed to work under
          the Mantra:
          >
          VB = Bad;
          VC# = Good.
          >
          :-)
          >
          So, if I can do it, I'd like to not use the VB namespace.
          >
          John
          >

          Comment

          • Willy Denoyette [MVP]

            #6
            Re: String Formatting (Like in VB)

            See inline.

            Willy.

            <jmanion@fort e-industries.comw rote in message
            news:1159209381 .511882.278180@ e3g2000cwe.goog legroups.com...
            | Hello,
            |
            | I have a problem, I'm hoping someone can help.
            |
            | I have a string, lets say: "111111"; // Not always numeric
            | I have a format mask, lets say: "(00) 0000"; // Again, not always
            | numeric.
            |

            Not sure what you mean with one/both not always being numeric.
            But here is how you can format a string.

            Here suppose the Mask is ("(000) 0000") and the string something like
            "1234567" or " 123.2356" or " +1.234567 " ...


            private string GiveMeTheFormat tedString(strin g i_Value, string i_Mask)
            {
            double dv;
            if(Double.TryPa rse(i_Value, NumberStyles.Nu mber,null, out dv))
            return dv.ToString(i_M aks);
            else return null;
            }

            If this doesn't suits your needs you will have to implement your own
            IFormatProvider .



            | The mask and the value are passed into a method.
            |
            | private string GiveMeTheFormat tedString(strin g i_Value, string i_Mask)
            | {
            |
            | }
            |
            | I want the output of this method to be: "(11) 1111"
            |
            | Remember, I can't assume that my input string or my mask will be
            | numeric and the mask will be different.
            |
            | Help. :-)
            |
            | I've tried this:
            |
            | string retVal = String.Format(" {0:" + iMask + "}", i_Value);
            |
            | No luck, it always comes back as the original input.
            |
            | I even tried the above hard coding the format mask in, i.e. ("{0:(00)
            | 0000}",i_Value) ;
            |
            | still no luck.
            |
            | Thanks for the help, in advance.
            |
            | John
            |


            Comment

            • Larry Lard

              #7
              Re: String Formatting (Like in VB)

              jmanion@forte-industries.com wrote:
              Hello,
              >
              I have a problem, I'm hoping someone can help.
              >
              I have a string, lets say: "111111"; // Not always numeric
              I have a format mask, lets say: "(00) 0000"; // Again, not always
              numeric.
              >
              The mask and the value are passed into a method.
              >
              private string GiveMeTheFormat tedString(strin g i_Value, string i_Mask)
              {
              >
              }
              >
              I want the output of this method to be: "(11) 1111"
              >
              Remember, I can't assume that my input string or my mask will be
              numeric and the mask will be different.
              OK, so what do you want the output to be in these cases:

              input: abcdef
              mask: (00) 0000

              input: -3.14592536
              mask: (0) 0-0 (0)

              input: 1A2B3C
              mask: 00abcdef

              ?

              IF your input is an integer and you want to use a custom numeric format
              string, cast to an integer and use ToString():

              string formatted = 111111.ToString ("(00) 0000");
              // now formatted == "(11) 1111"

              To be able to say how to produce results in other cases, we would need
              to know what you want to see in those other cases.



              --
              Larry Lard
              larrylard@googl email.com
              The address is real, but unread - please reply to the group
              For VB and C# questions - tell us which version

              Comment

              Working...