title: How can I split tag number of rfid as integer?
body: <p>I'm getting the information by reading the card, but how can I just get the tag number as int?</p>
  
  <pre><code>        public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              if (!serialPort1.IsOpen)
              {
                  serialPort1.Open();
                  serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
              }
  
              MessageBox.Show("Success");
          }
          private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
          {
              Control.CheckForIllegalCrossThreadCalls = false;
              SerialPort sp = (SerialPort)sender;
              string indata = sp.ReadExisting();
              textRFID.Text += indata;
          }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/DWM6z.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DWM6z.jpg" alt="rfid-ss"></a></p>
  
  <p>This is the string I need to parse: </p>
  
  <pre><code>"RFID tag detected Tag Type:\tMifare One (S50) The tag's number is:\t235717311 Read Checksum:\t238 Calculated Checksum:\t238"
  </code></pre>
  
  <p>Thanks to @haldo I can receive my tag number but it gives "object reference not set to an instance of an object" error and I put try catch, when I press ok to error, it goes and output is as I wanted. How can we remove this error?</p>
  
  <p>Here is my editing code :</p>
  
  <pre><code>private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
          {
              Control.CheckForIllegalCrossThreadCalls = false;
              string contains = "The tag's number is:";
              SerialPort sp = (SerialPort)sender;
              string indata = sp.ReadExisting();
              int tagNumber = 0;
              try
              {
                  indata.Split(Environment.NewLine.ToCharArray())      // split on newline chars
                        .FirstOrDefault(s =&gt; s.Contains(contains)) // get first string matching pattern above 
                        .Split(':')                                    // split on ':'
                        .FirstOrDefault(x =&gt; int.TryParse(x, out tagNumber));  // return first successful try parse
              }
              catch (Exception ex) { MessageBox.Show(ex.Message); }
              textRFID.Text = tagNumber.ToString();            
          }
  </code></pre>