none
Trouble converting data from a readline, via Getstream from a tcpclient listerner into indiviual vars RRS feed

  • Question

  • Hi,

    I have written a TCPCLIENT listener, and am able to get it to display the output on screen in real-time.

    What I am now trying to do it take the incoming data and split it into individual vars.

    I'm using the string.split method, but I am only getting the 1st var/"split" out

    My data comes out like this :

    22/12/17 08:44AM   201 0002 <D>01234567890<I>07412345678                                     �00000.00              RC  
    22/12/17 08:44AM   201 0002 <D>01234567890<I>07412345678                                     0'03 00:00'00 �00000.00              NA  

    My code looks like this

    while (client.Connected) { string DataReceived = reader.ReadLine(); Console.WriteLine(DataReceived); string[] indiVars = DataReceived.Split(new char[] { ' ' }); string FirstVar = indiVars[0]; string SecondVar = indiVars[1];

    //add more strings for other data in datareceived Console.WriteLine(FirstVar, SecondVar); }



    Could someone point out my error? Do I need to incase the split procedures into an IF loop?

    Thanks

    Friday, December 22, 2017 9:17 AM

Answers

  • [...]

    Console.WriteLine(result.FirstVar, result.SecondVar);
    Console.WriteLine(result);

    the first Console.Writeline only prints out result.FirstVar ( as the original code did ) [...]



    To display both of values, try this:

       Console.WriteLine("{0} {1}", result.FirstVar, result.SecondVar);


    • Edited by Viorel_MVP Friday, December 22, 2017 9:51 PM
    • Marked as answer by G-Oker Saturday, December 23, 2017 9:19 AM
    Friday, December 22, 2017 9:50 PM

All replies

  • UPDATE:

    I've tried

    string[] indiVars = DataReceived.Split(new char[] { ' ' });
    
    var result = new
     {
      FirstVar = indiVars[0],
      SecondVar = indiVars[1],
      };
    
    Console.WriteLine(result.FirstVar, result.SecondVar);
    Console.WriteLine(result);

    the first Console.Writeline only prints out result.FirstVar ( as the original code did ), and if I swap them around Ie, the line reads

    Console.WriteLine(result.SecondVar, result.FirstVar);

    then result.SecondVar, but not result.FirstVar.

    Console.WriteLine(result); prints out both variables, but I am not sure I can use this to then parse onto an SQL command


    • Edited by G-Oker Friday, December 22, 2017 11:43 AM
    Friday, December 22, 2017 11:12 AM
  • Are you sure that the separate is a space and not a tab? Take a look at the chars in the string that come in and see if those are tabs between the fields (a tab separated value) instead of just spaces. Split allows a series of characters so you can switch to something like this instead.

    var indiVars = DataReceived.Split(' ', '\t');
    
    

    Then take a look at the Length property of the indiVars to see how many you're getting back. If it is still not splitting correctly then take a look at the actual characters in the string to see what other characters it may be using in addition to whitespace.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, December 22, 2017 6:04 PM
    Moderator
  • [...]

    Console.WriteLine(result.FirstVar, result.SecondVar);
    Console.WriteLine(result);

    the first Console.Writeline only prints out result.FirstVar ( as the original code did ) [...]



    To display both of values, try this:

       Console.WriteLine("{0} {1}", result.FirstVar, result.SecondVar);


    • Edited by Viorel_MVP Friday, December 22, 2017 9:51 PM
    • Marked as answer by G-Oker Saturday, December 23, 2017 9:19 AM
    Friday, December 22, 2017 9:50 PM
  • D'oh... how could I have missed that ???

    thank you, Viorel

    Saturday, December 23, 2017 9:19 AM