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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Split a String of numbers in textfield

Split a String of numbers in textfield

Scheduled Pinned Locked Moved C#
csharplinqgraphicsiotdata-structures
5 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.
  • A Offline
    A Offline
    auting82
    wrote on last edited by
    #1

    I have 5 randomly generated numbers type in text field every time I press a button: Something like this: 0.5670.9870.3420.5210.07 etc......They represent sensor values in range 0-1V I would like to split them so that I get something like this: 0.567 0.987 0.342 0.521 0.07 Also I would like to add a line shift so that every time I press the button the number series goes onto next line. Anybody has an idea ? This is my main Form1.cs class:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace DAQ_Simulator
    {
    public partial class Form1 : Form
    {
    int counter;

        private DateTime datetime;
        private DateTime datetime2;
        //double next\_samplingtime += DateTime.Now;
        //int maxSid = 10; //Number of sensors 7analog and 3 digital
        int maxAI = 7; 
        int maxDI=3;
        int maxSid =10;
        
    
        // string sensor\_values;
        string sTxt; //get analog values
        string sTxt1; //get digital states
    
    
        // Create an array of 10 sensor objects
        Sensor\[\] sObj = new Sensor\[10\];
    
        //DateTime dt = new DateTime(2018, 2, 8, 14, 22, 30, 123);
    
    
        public Form1()
        {
            InitializeComponent();
    
            for (counter = 0; counter < maxSid; counter++)
            {
                sObj\[counter\] = new Sensor(counter);
            }
        }
        private void displaySensorData(object sender, EventArgs e)
        {
        }
        private void groupSampl\_Enter(object sender, EventArgs e)
        {
    
        }
        private void textSampling\_TextChanged(object sender, EventArgs e)
        {
    
    
    
        }
        private void btnSampling\_Click(object sender, EventArgs e)
        {
            timer1.Interval = 5900; // here in miliseconds
            timer1.Start();
            btnSampling.Enabled = false;
            sampling();
            
    
            if (textSampling.Text == "")
            {
                textSampling.Text = "Sample";
            }
    
            datetime2 = DateTime.Now.AddSeconds(5).AddMilliseconds(900);
            string time2 = datetime2.Hour + ":" + datetime2.Minute + ":" + datetime2.Second + "." + datetime2.Millisecond;
            textSampling.Text =time2;
            System.TimeSpan diff1 = datetime2.Subtract(datetime);
    
    L L 2 Replies Last reply
    0
    • A auting82

      I have 5 randomly generated numbers type in text field every time I press a button: Something like this: 0.5670.9870.3420.5210.07 etc......They represent sensor values in range 0-1V I would like to split them so that I get something like this: 0.567 0.987 0.342 0.521 0.07 Also I would like to add a line shift so that every time I press the button the number series goes onto next line. Anybody has an idea ? This is my main Form1.cs class:

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;

      namespace DAQ_Simulator
      {
      public partial class Form1 : Form
      {
      int counter;

          private DateTime datetime;
          private DateTime datetime2;
          //double next\_samplingtime += DateTime.Now;
          //int maxSid = 10; //Number of sensors 7analog and 3 digital
          int maxAI = 7; 
          int maxDI=3;
          int maxSid =10;
          
      
          // string sensor\_values;
          string sTxt; //get analog values
          string sTxt1; //get digital states
      
      
          // Create an array of 10 sensor objects
          Sensor\[\] sObj = new Sensor\[10\];
      
          //DateTime dt = new DateTime(2018, 2, 8, 14, 22, 30, 123);
      
      
          public Form1()
          {
              InitializeComponent();
      
              for (counter = 0; counter < maxSid; counter++)
              {
                  sObj\[counter\] = new Sensor(counter);
              }
          }
          private void displaySensorData(object sender, EventArgs e)
          {
          }
          private void groupSampl\_Enter(object sender, EventArgs e)
          {
      
          }
          private void textSampling\_TextChanged(object sender, EventArgs e)
          {
      
      
      
          }
          private void btnSampling\_Click(object sender, EventArgs e)
          {
              timer1.Interval = 5900; // here in miliseconds
              timer1.Start();
              btnSampling.Enabled = false;
              sampling();
              
      
              if (textSampling.Text == "")
              {
                  textSampling.Text = "Sample";
              }
      
              datetime2 = DateTime.Now.AddSeconds(5).AddMilliseconds(900);
              string time2 = datetime2.Hour + ":" + datetime2.Minute + ":" + datetime2.Second + "." + datetime2.Millisecond;
              textSampling.Text =time2;
              System.TimeSpan diff1 = datetime2.Subtract(datetime);
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, rather than struggling with a problem you created yourself (see the line with sTxt+=...getAnalogValue...) you should try and avoid the problem from the start. I have several ideas: - don’t concatenate numbers in a string without a separator; by adding a space or some symbol (say #) you could ease the string splitting later on (have a look at the string.Split method!) - much better yet, don’t confuse your “business logic” with your user interface, i.e. store your measurement results one by one in an appropriate data structure (could be a List or so), and do whatever is required for displaying separately. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      A 1 Reply Last reply
      0
      • A auting82

        I have 5 randomly generated numbers type in text field every time I press a button: Something like this: 0.5670.9870.3420.5210.07 etc......They represent sensor values in range 0-1V I would like to split them so that I get something like this: 0.567 0.987 0.342 0.521 0.07 Also I would like to add a line shift so that every time I press the button the number series goes onto next line. Anybody has an idea ? This is my main Form1.cs class:

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows.Forms;

        namespace DAQ_Simulator
        {
        public partial class Form1 : Form
        {
        int counter;

            private DateTime datetime;
            private DateTime datetime2;
            //double next\_samplingtime += DateTime.Now;
            //int maxSid = 10; //Number of sensors 7analog and 3 digital
            int maxAI = 7; 
            int maxDI=3;
            int maxSid =10;
            
        
            // string sensor\_values;
            string sTxt; //get analog values
            string sTxt1; //get digital states
        
        
            // Create an array of 10 sensor objects
            Sensor\[\] sObj = new Sensor\[10\];
        
            //DateTime dt = new DateTime(2018, 2, 8, 14, 22, 30, 123);
        
        
            public Form1()
            {
                InitializeComponent();
        
                for (counter = 0; counter < maxSid; counter++)
                {
                    sObj\[counter\] = new Sensor(counter);
                }
            }
            private void displaySensorData(object sender, EventArgs e)
            {
            }
            private void groupSampl\_Enter(object sender, EventArgs e)
            {
        
            }
            private void textSampling\_TextChanged(object sender, EventArgs e)
            {
        
        
        
            }
            private void btnSampling\_Click(object sender, EventArgs e)
            {
                timer1.Interval = 5900; // here in miliseconds
                timer1.Start();
                btnSampling.Enabled = false;
                sampling();
                
        
                if (textSampling.Text == "")
                {
                    textSampling.Text = "Sample";
                }
        
                datetime2 = DateTime.Now.AddSeconds(5).AddMilliseconds(900);
                string time2 = datetime2.Hour + ":" + datetime2.Minute + ":" + datetime2.Second + "." + datetime2.Millisecond;
                textSampling.Text =time2;
                System.TimeSpan diff1 = datetime2.Subtract(datetime);
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Why are you converting numbers to strings and then concatenating them all into an unparsable list? The only time you need to convert them to strings is when displaying or printing. Inside your application you should keep them as doubles at all times.

        1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, rather than struggling with a problem you created yourself (see the line with sTxt+=...getAnalogValue...) you should try and avoid the problem from the start. I have several ideas: - don’t concatenate numbers in a string without a separator; by adding a space or some symbol (say #) you could ease the string splitting later on (have a look at the string.Split method!) - much better yet, don’t confuse your “business logic” with your user interface, i.e. store your measurement results one by one in an appropriate data structure (could be a List or so), and do whatever is required for displaying separately. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          A Offline
          A Offline
          auting82
          wrote on last edited by
          #4

          Hi, thanks for the advice.I am new to programming and as you can see don't have too much experience. I apologize if my questions seem "stupid". Back to this problem, I managed to "solve" the problem but I don't think the solution is good even though it works when I run the program. The instructor of this class I am taking told us to use for loops to solve this problem of getting sensor values and ID numbers of the sensors. I did not do that. I get completely lost when I need to create an array for sensor values and match it with sensor IDs. Another thing I am struggling with is how to compare dates. I have a time stamp that indicates clock-time for every sample. A sample time is 5,seconds which means I have to disable Sample button for 5,seconds after clicking it. I used Date.Time.Now and tried to compare dates, I basically have to times one that is current and other that is 5,9 seconds ahead., but since both clocks are running I am not able to apply if/else statement to compare them. Would you please take a look at this code and let me know if there is any nicer way of solving this preferably with loops.

          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Windows.Forms;

          namespace DAQ_Simulator
          {
          public partial class Form1 : Form
          {
          int counter;
          private DateTime datetime;
          private DateTime datetime2;
          //double next_samplingtime += DateTime.Now;
          //int maxSid = 10; //Number of sensors 7analog and 3 digital
          int maxAI = 7;
          int maxDI=3;
          int maxSid =11;

              // Create an array of 10 sensor objects
              Sensor\[\] sObj = new Sensor\[11\];
          
              public Form1()
              {
                  InitializeComponent();
          
                  for (counter = 0; counter < maxSid; counter++)
                  {
                      sObj\[counter\] = new Sensor(counter);
                  }
              }
              private void displaySensorData(object sender, EventArgs e)
              {
              }
              private void groupSampl\_Enter(object sender, EventArgs e)
              {
          
              }
              private void textSampling\_TextChanged(object sender, EventArgs e)
              {
              }
              private void btnSampling\_Click(object sender, EventArgs e)
              {
                  timer1.Interval = 5900; // sampling time specified in miliseconds
                  timer1.Start();
                  btnSampling.Enabled = false
          
          L 1 Reply Last reply
          0
          • A auting82

            Hi, thanks for the advice.I am new to programming and as you can see don't have too much experience. I apologize if my questions seem "stupid". Back to this problem, I managed to "solve" the problem but I don't think the solution is good even though it works when I run the program. The instructor of this class I am taking told us to use for loops to solve this problem of getting sensor values and ID numbers of the sensors. I did not do that. I get completely lost when I need to create an array for sensor values and match it with sensor IDs. Another thing I am struggling with is how to compare dates. I have a time stamp that indicates clock-time for every sample. A sample time is 5,seconds which means I have to disable Sample button for 5,seconds after clicking it. I used Date.Time.Now and tried to compare dates, I basically have to times one that is current and other that is 5,9 seconds ahead., but since both clocks are running I am not able to apply if/else statement to compare them. Would you please take a look at this code and let me know if there is any nicer way of solving this preferably with loops.

            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows.Forms;

            namespace DAQ_Simulator
            {
            public partial class Form1 : Form
            {
            int counter;
            private DateTime datetime;
            private DateTime datetime2;
            //double next_samplingtime += DateTime.Now;
            //int maxSid = 10; //Number of sensors 7analog and 3 digital
            int maxAI = 7;
            int maxDI=3;
            int maxSid =11;

                // Create an array of 10 sensor objects
                Sensor\[\] sObj = new Sensor\[11\];
            
                public Form1()
                {
                    InitializeComponent();
            
                    for (counter = 0; counter < maxSid; counter++)
                    {
                        sObj\[counter\] = new Sensor(counter);
                    }
                }
                private void displaySensorData(object sender, EventArgs e)
                {
                }
                private void groupSampl\_Enter(object sender, EventArgs e)
                {
            
                }
                private void textSampling\_TextChanged(object sender, EventArgs e)
                {
                }
                private void btnSampling\_Click(object sender, EventArgs e)
                {
                    timer1.Interval = 5900; // sampling time specified in miliseconds
                    timer1.Start();
                    btnSampling.Enabled = false
            
            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, I have lots of comments, here are just a few: 1. I don't think Sensor.GetAnalogValue() is doing what you think it is doing, I fail to see how the current code could be useful. Maybe you should first add a comment explaining what the method is supposed to do, and only then compare the code with the written description. 2. I see two new Random() statements inside Sensor. They tell me you did not understand the matter that was handled in your earlier thread, two days ago. This is what the Random class does: - each NextValue() returns a value assumed to be random, however it is never random, it is pseudo-random, which means anyone knowing the internal code can predict the next value. The values returned are from a huge but fixed and circular list (not stored but calculated), and the only random thing about it is you can influence where in the list the sequence will start, that is what the "seed" does. - If you provide the same seed to different Random instances, you get the same values returned. - And if you don't, the .NET Random class will derive a seed from the current time with millisecond resolution. As creating a Random object takes much less than a millisecond, two consecutive new Random() will typically use the same seed. Normally, the easy way to correctly use Random is by having only one of them, so make it a static field within your Sensor class. 3. If you already know what Properties are, you shouldn't have a method called GetSensId(). If properties still are in your future, then don't bother for now. 4. Having a lot of variables with almost identical names (sensVal#, analogSens#) in general indicates bad design; you should consider avoiding so many variables in the first place, and when you really need them all at the same time, then consider an aggregate, such as an Array. Doing so your code will turn out to be more concise, clearer, and less prone to typing errors. 5. A method that is intended to obtain some values, such as sampling(), normally should make those values available to the other methods and properties of the class; yours doesn't, it has type void, and the only thing coming out of it is a complex string going to the screen. There are many ways to provide values, the obvious one is by using a return statement (you could return an Array of floats, of a List of doubles, or whatever suits you); alternatives are "out" parameters (don't abuse those!), and in some cases class-level variables (not recommended here). I hope you notice how this fits well with the previous

            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