Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help me with formatted int32 strings. [modified]

Help me with formatted int32 strings. [modified]

Scheduled Pinned Locked Moved C#
help
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Solid Green

    Thank you.:) I do not have a good idea to convert string ABCDE-0000012345 to int32 :(( Especially when the format string is "ABCDE-0066000880"

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    What kind of strings are that ? Do you mean : ABCDE-0000012345 = 12345 ABCDE-0066000880 = 66000880 ???

    S 1 Reply Last reply
    0
    • S Solid Green

      We can use int32.TryPrase("1231312",out tmpInt32Value) for normal situtations. But the following code, string outputString = (12345).ToString("ABCDE-0000000000"); I do not have a good idea to read such string ABCDE-0000012345 :(( Especially when the format string is "ABCDE-0066000880"

      modified on Tuesday, May 26, 2009 3:09 AM

      T Offline
      T Offline
      Tuwing Sabado
      wrote on last edited by
      #5

      Your going to format your interger to a predefine format. Example 1 string outputString = (12345).ToString("ABCDE-0000000000"); //Output: "ABCDE0000000000" Example 2 string outputString = (12345).ToString("ABCDED-0000000000"); //Output: "ABCDED-0000012345" If your are expecting that the output of Example 1 is ABCDE-0000012345 you are wrong because "E-0" means Scientific notation. Note: If any of the strings "E", "E+", "E-", "e", "e+", or "e-" are present in the format string and are followed immediately by at least one '0' character, then the number is formatted using scientific notation with an 'E' or 'e' inserted between the number and the exponent. The number of '0' characters following the scientific notation indicator determines the minimum number of digits to output for the exponent. The "E+" and "e+" formats indicate that a sign character (plus or minus) should always precede the exponent. The "E", "E-", "e", or "e-" formats indicate that a sign character should only precede negative exponents. See Custom Numeric Format String[^ for more details. Happing Coding...

      S 1 Reply Last reply
      0
      • L Lost User

        What kind of strings are that ? Do you mean : ABCDE-0000012345 = 12345 ABCDE-0066000880 = 66000880 ???

        S Offline
        S Offline
        Solid Green
        wrote on last edited by
        #6

        when the format string is "ABCDE-000066000880"; int 1234 custom formated value is "ABCDE000066123884" My question is how to get the int value by custom format string "ABCDE-000066000880" and formatted string "ABCDE000066123884". :)

        1 Reply Last reply
        0
        • T Tuwing Sabado

          Your going to format your interger to a predefine format. Example 1 string outputString = (12345).ToString("ABCDE-0000000000"); //Output: "ABCDE0000000000" Example 2 string outputString = (12345).ToString("ABCDED-0000000000"); //Output: "ABCDED-0000012345" If your are expecting that the output of Example 1 is ABCDE-0000012345 you are wrong because "E-0" means Scientific notation. Note: If any of the strings "E", "E+", "E-", "e", "e+", or "e-" are present in the format string and are followed immediately by at least one '0' character, then the number is formatted using scientific notation with an 'E' or 'e' inserted between the number and the exponent. The number of '0' characters following the scientific notation indicator determines the minimum number of digits to output for the exponent. The "E+" and "e+" formats indicate that a sign character (plus or minus) should always precede the exponent. The "E", "E-", "e", or "e-" formats indicate that a sign character should only precede negative exponents. See Custom Numeric Format String[^ for more details. Happing Coding...

          S Offline
          S Offline
          Solid Green
          wrote on last edited by
          #7

          Thank you for your answer. And ,i made a bad formatted sample. Sorry to everyone. we know that code (1234).ToString("ABCD-000066000880") 's value is ABCD-000066123884. And I want to get a int value from string "ABCD-000066123884" and formatted string "ABCD-000066000880"。

          T 1 Reply Last reply
          0
          • S Solid Green

            Thank you for your answer. And ,i made a bad formatted sample. Sorry to everyone. we know that code (1234).ToString("ABCD-000066000880") 's value is ABCD-000066123884. And I want to get a int value from string "ABCD-000066123884" and formatted string "ABCD-000066000880"。

            T Offline
            T Offline
            Tuwing Sabado
            wrote on last edited by
            #8

            if your string format is constant to have a format like this "ABCD-0000000000", try to split your string to string array then get the second index value and lastly do your Int32.TryParse thing. string[] myString = ("ABCD-000066123884").Split('-'); Int32.TryParse(myString[1],out intVar); Happy Coding...

            L 1 Reply Last reply
            0
            • T Tuwing Sabado

              if your string format is constant to have a format like this "ABCD-0000000000", try to split your string to string array then get the second index value and lastly do your Int32.TryParse thing. string[] myString = ("ABCD-000066123884").Split('-'); Int32.TryParse(myString[1],out intVar); Happy Coding...

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              I think I know what he wants : That means : ABCD-000066123884 is not 66123884, but 1234. If that is the problem, then you must write your own method to parse the string.

              S 1 Reply Last reply
              0
              • L Lost User

                I think I know what he wants : That means : ABCD-000066123884 is not 66123884, but 1234. If that is the problem, then you must write your own method to parse the string.

                S Offline
                S Offline
                Solid Green
                wrote on last edited by
                #10

                Yes , It is the problem. :((

                L 1 Reply Last reply
                0
                • S Solid Green

                  Yes , It is the problem. :((

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #11

                  Probably this is what you need:

                  static int MyOwnParser(string text, string formatter)
                  {
                  StringBuilder stringBuilder = new StringBuilder();
                  for (int i = 0; i < formatter.Length; i++ )
                  {
                  if(formatter[i] == '0')
                  {
                  stringBuilder.Append(text[i]);
                  }
                  }

                  return int.Parse(stringBuilder.ToString());
                  }

                  G 1 Reply Last reply
                  0
                  • L Lost User

                    Probably this is what you need:

                    static int MyOwnParser(string text, string formatter)
                    {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < formatter.Length; i++ )
                    {
                    if(formatter[i] == '0')
                    {
                    stringBuilder.Append(text[i]);
                    }
                    }

                    return int.Parse(stringBuilder.ToString());
                    }

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #12

                    I was thinking in the same direction, only without the StringBuilder as intermediate result...

                    int result = 0;
                    for (int i = 0; i < formatter.Length; i++) {
                    if (formatter[i] == '0') {
                    result = result * 10 + (text[i] - '0');
                    }
                    }

                    Despite everything, the person most likely to be fooling you next is yourself.

                    S 1 Reply Last reply
                    0
                    • G Guffa

                      I was thinking in the same direction, only without the StringBuilder as intermediate result...

                      int result = 0;
                      for (int i = 0; i < formatter.Length; i++) {
                      if (formatter[i] == '0') {
                      result = result * 10 + (text[i] - '0');
                      }
                      }

                      Despite everything, the person most likely to be fooling you next is yourself.

                      S Offline
                      S Offline
                      Solid Green
                      wrote on last edited by
                      #13

                      This method of int32 convert to string with format,maybe can not reverse. When the format string contains "#". From MSDN: # Digit placeholder If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string. Note that this specifier never displays the '0' character if it is not a significant digit, even if '0' is the only digit in the string. It will display the '0' character if it is a significant digit in the number being displayed. The "##" format string causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "##" would result in the value 35.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups