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 need help splitting '123456' into ['1','2','3','4','5','6']

i need help splitting '123456' into ['1','2','3','4','5','6']

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 5 Posters 4 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.
  • N Offline
    N Offline
    NZSmartie
    wrote on last edited by
    #1

    Hey how can i split a string into individual characters... my input string is always a set of numbers like '15323' and i want to split it up and add all the numbers together so i end up with a total of 14 (from before). Roman

    C G R S 4 Replies Last reply
    0
    • N NZSmartie

      Hey how can i split a string into individual characters... my input string is always a set of numbers like '15323' and i want to split it up and add all the numbers together so i end up with a total of 14 (from before). Roman

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You can use array syntax to iterate over the characters in a string, use int.TryParse to turn them into numbers, and add them. Something like this string input = "12324590234" int total = 0; foreach(char c in input) // oh yeah, I reckon this will work, too { int next = 0; if(int.TryParse(c.ToString(), out next)) { total += next; } } MessageBox.Show(next.ToString()); That's off the cuff, but it should be close.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      1 Reply Last reply
      0
      • N NZSmartie

        Hey how can i split a string into individual characters... my input string is always a set of numbers like '15323' and i want to split it up and add all the numbers together so i end up with a total of 14 (from before). Roman

        G Offline
        G Offline
        gauthee
        wrote on last edited by
        #3

        if you are building that string dynamically try to add a character like',' in between, so your string should be"1,2,3,4,5". If your string is this way then we could easily convert it to an array using the split method... example: string str1="1,2,3,4,5"; then str1.split(',') ....later you can use a for loop and add the elements in the array.....

        Gautham

        1 Reply Last reply
        0
        • N NZSmartie

          Hey how can i split a string into individual characters... my input string is always a set of numbers like '15323' and i want to split it up and add all the numbers together so i end up with a total of 14 (from before). Roman

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          Hi, char[] chars = "123456".ToCharArray(); Robert

          1 Reply Last reply
          0
          • N NZSmartie

            Hey how can i split a string into individual characters... my input string is always a set of numbers like '15323' and i want to split it up and add all the numbers together so i end up with a total of 14 (from before). Roman

            S Offline
            S Offline
            saravanan_raju
            wrote on last edited by
            #5

            hi , hope the below codesnippet will satisfy your needs string s ="123456"; int total=0; for(int i =0 ; i <s.Length ;i++> ) { int m = Convert.ToInt32(s[i].ToString()); total += m; } Thanks Saravanan R

            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