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. binning at zero-filling data

binning at zero-filling data

Scheduled Pinned Locked Moved C#
csharptutorialquestion
3 Posts 3 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 Offline
    S Offline
    shekarchee
    wrote on last edited by
    #1

    Hello, I would like to bin and zero-fill excel or text data using C#. For example; Raw data: 1 34 2 3 43 4 3 5 converted to: 1 34 2 0 3 45 4 3 5 0 Does anyone have code for this? Thanks

    L L 2 Replies Last reply
    0
    • S shekarchee

      Hello, I would like to bin and zero-fill excel or text data using C#. For example; Raw data: 1 34 2 3 43 4 3 5 converted to: 1 34 2 0 3 45 4 3 5 0 Does anyone have code for this? Thanks

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

      Couldn't you just format the column to be a number that was an empty cell would display 0?

      Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

      1 Reply Last reply
      0
      • S shekarchee

        Hello, I would like to bin and zero-fill excel or text data using C#. For example; Raw data: 1 34 2 3 43 4 3 5 converted to: 1 34 2 0 3 45 4 3 5 0 Does anyone have code for this? Thanks

        L Offline
        L Offline
        LookSharp
        wrote on last edited by
        #3

        For text data, look for lines that don't contain an embedded space, then append " 0":

        public List<string> CleanupText(List<string> inputLines)
        	{
        	List<string> outputLines = new List<string>();
        	foreach (string s in inputLines)
        		{
        		// Note: the Trim() and Replace() ensure that (a) there are no false hits and (b) no false misses, respectively
        		if (s.Trim().Replace('\\t', ' ').Contains(" "))
        			outputLines.Add(s);
        		else
        			outputLines.Add(string.Concat(s, " 0"));
        		}
        	return outputLines;
        	}
        

        If you want to modify the collection in place, you'll have to replace the foreach() with a for() and an index into the list - since foreach() loops disallow modifying the collection they are iterating. To do this with Excel you'll have to iterate rows (the end condition being a cell in the first column that is empty), looking for cells in the 2nd column that are blank and inserting a '0' into those blank cells. I've not done enough MSOffice interop to be able to give you a code example.

        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