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. I have string "1999220" how to parse?

I have string "1999220" how to parse?

Scheduled Pinned Locked Moved C#
tutorialquestion
8 Posts 6 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.
  • U Offline
    U Offline
    Unque
    wrote on last edited by
    #1

    I have develop simple application first I take birthday from user and calculate his lucky number from sum of the values of the birthday Ex: '19881205' How i get the sum of values in this string?

    L M A D 4 Replies Last reply
    0
    • U Unque

      I have develop simple application first I take birthday from user and calculate his lucky number from sum of the values of the birthday Ex: '19881205' How i get the sum of values in this string?

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

      Use one of the integer methods[^] to parse it into a number and then use the Digital root method[^]. Alternatively extract each character one at a time, and sum the integer values.

      Use the best guess

      U 1 Reply Last reply
      0
      • U Unque

        I have develop simple application first I take birthday from user and calculate his lucky number from sum of the values of the birthday Ex: '19881205' How i get the sum of values in this string?

        M Offline
        M Offline
        M Badger
        wrote on last edited by
        #3

        Long winded, but as a demo...

        static void SumNumbersinDate()
        {
        //http://www.codeproject.com/Messages/4513781/I-have-string-1999220-how-to-parse.aspx
        string theDate = "19881205";
        char[] dateLetters = theDate.ToCharArray();
        int[] dateNumbers = new int[theDate.Length];
        int sum = 0;
        for (int ind = 0; ind < theDate.Length; ind++)
        {
        Console.WriteLine("Current Total: " + sum.ToString());
        dateNumbers[ind] = int.Parse(dateLetters[ind].ToString());
        Console.WriteLine("Adding: " + dateNumbers[ind].ToString());
        sum += dateNumbers[ind];
        Console.WriteLine("New Total: " + sum.ToString());
        }
        Console.WriteLine("The sum of the numbers is: " + sum.ToString());
        Console.WriteLine("Press any key to close the demo...");
        Console.ReadKey(true);
        }

        U 1 Reply Last reply
        0
        • L Lost User

          Use one of the integer methods[^] to parse it into a number and then use the Digital root method[^]. Alternatively extract each character one at a time, and sum the integer values.

          Use the best guess

          U Offline
          U Offline
          Unque
          wrote on last edited by
          #4

          Thank you

          1 Reply Last reply
          0
          • M M Badger

            Long winded, but as a demo...

            static void SumNumbersinDate()
            {
            //http://www.codeproject.com/Messages/4513781/I-have-string-1999220-how-to-parse.aspx
            string theDate = "19881205";
            char[] dateLetters = theDate.ToCharArray();
            int[] dateNumbers = new int[theDate.Length];
            int sum = 0;
            for (int ind = 0; ind < theDate.Length; ind++)
            {
            Console.WriteLine("Current Total: " + sum.ToString());
            dateNumbers[ind] = int.Parse(dateLetters[ind].ToString());
            Console.WriteLine("Adding: " + dateNumbers[ind].ToString());
            sum += dateNumbers[ind];
            Console.WriteLine("New Total: " + sum.ToString());
            }
            Console.WriteLine("The sum of the numbers is: " + sum.ToString());
            Console.WriteLine("Press any key to close the demo...");
            Console.ReadKey(true);
            }

            U Offline
            U Offline
            Unque
            wrote on last edited by
            #5

            Thank you

            1 Reply Last reply
            0
            • U Unque

              I have develop simple application first I take birthday from user and calculate his lucky number from sum of the values of the birthday Ex: '19881205' How i get the sum of values in this string?

              A Offline
              A Offline
              Abhinav S
              wrote on last edited by
              #6

              Integer.TryParse is often the best solution since it ensures you are parsing an integer and if not, does not throw an error.

              WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

              1 Reply Last reply
              0
              • U Unque

                I have develop simple application first I take birthday from user and calculate his lucky number from sum of the values of the birthday Ex: '19881205' How i get the sum of values in this string?

                D Offline
                D Offline
                dusty_dex
                wrote on last edited by
                #7

                Do you also have to verify that a date is valid? I'd hazard a guess that maybe you should. Garbage-In = Garbage-Out

                Q. Hey man! have you sorted out the finite soup machine? A. Why yes, it's celery or tomato.

                M 1 Reply Last reply
                0
                • D dusty_dex

                  Do you also have to verify that a date is valid? I'd hazard a guess that maybe you should. Garbage-In = Garbage-Out

                  Q. Hey man! have you sorted out the finite soup machine? A. Why yes, it's celery or tomato.

                  M Offline
                  M Offline
                  Manfred Rudolf Bihy
                  wrote on last edited by
                  #8

                  Since OP needs this to calculate a "lucky number" I wouldn't worry too much about "garbage". ;)

                  "I had the right to remain silent, but I didn't have the ability!"

                  Ron White, Comedian

                  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