Results 1 to 8 of 8

Thread: when split msg variable to more lines it not give me values in lines?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    when split msg variable to more lines it not give me values in lines?

    Problem

    When split msg variable to more lines it not give me values in lines ?
    msg variable in debug give me text scanning bellow

    Details

    i work in windows form c# vs 2015

    i using bar code reader to read qr code

    bar code scanner working as USB keyboard and define as HID Drivers

    if i replace msg variable with text box it working in read data and spiting

    SUCCESS so that

    what is the problem in splitting below code ?

    text scanning :

    30 General Conference of Arab Pharmaceutical Unions

    UserName : khalid ramzy

    Country : Saudia

    Membership : part

    Serial : 1014

    my code




    Code:
    public partial class Form1 : Form
        {
            DateTime _lastKeystroke = new DateTime(0);
            List<char> _barcode = new List<char>(10);
            public Form1()
            {
                InitializeComponent();
    
            }
    
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
                TimeSpan elapsed = (DateTime.Now - _lastKeystroke);
                if (elapsed.TotalMilliseconds > 100)
                    _barcode.Clear();
    
                // record keystroke & timestamp
                _barcode.Add(e.KeyChar);
                _lastKeystroke = DateTime.Now;
    
                // process barcode
                if (e.KeyChar == 13 && _barcode.Count > 0)
                {
    
                        string msg = new String(_barcode.ToArray());
    
                    string[] lines = msg.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
    
                    if (lines.Length > 5)
                    {
                        string msg1 = lines[1].Substring(lines[1].IndexOf(":") + 1);
                        label3.Text = msg1;
                        string msg2 = lines[2].Substring(lines[2].IndexOf(":") + 1);
                        label4.Text = msg2;
                        string msg3 = lines[3].Substring(lines[3].IndexOf(":") + 1);
                        label5.Text = msg3;
                        string msg4 = lines[4].Substring(lines[4].IndexOf(":") + 1);
                        label6.Text = msg4;
                        label2.Text = lines[5].Substring(lines[5].IndexOf(":") + 1);
                    }
                        _barcode.Clear();
                }
            }
        }
    }
    msg variable in debug have values of scanning qr code

    but problem now

    Code:
     How to split them to lines as above ?

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: when split msg variable to more lines it not give me values in lines?

    Psychic guess.

    "NewLine" means different things on different machines. On some machines it's CR (13). On other machines it's LF (10). Some machines use CR+LF (13 + 10). Some machines use LF + CR (10 + 13).

    Windows uses CR + LF (13 + 10) because that's what typewriters used, and it's always been extremely common for people to connect typewriters to their computer (You've probably got 12 typewriters at your desk, right?) Unix-style machines tend to use LF (10) because they felt it was more important to save a little memory than to support typewriters. Unix-style machines are also the most common servers, so many internet protocols consider LF the de facto line terminator. Nothing forces a program running on Windows to use CRLF, it's free to use LF or CR or even something more exotic if it wants.

    Your code only looks for CR (13). Your code splits based on Environment.NewLine, which is CRLF on a Windows machine. What if your barcode scanner is using CR, LF, or LFCR? If that happens, you may not even see a CR (13), and Environment.NewLine won't split along the line boundaries like you expect.

    You could inspect your input and figure out which newline character the barcode is actually using. OR, you could write your code to try to detect that newline character itself, and split accordingly:

    Code:
    public class Program
    {
    	public static void Main()
    	{
    		string input = "Hello\r world!";
    		string newlineToken = DetectNewline(input);
    		
    		var lines = Regex.Split(input, newlineToken);
    		foreach (var line in lines) {
    			Console.WriteLine(line);
    		}		
    	}
    	
    	private const char CR = '\u000D';
    	private const char LF = '\u000A';
    	
    	public static string DetectNewline(string input) {
    		var crIndex = input.IndexOf(CR);
    		var lfIndex = input.IndexOf(LF);
    		
    		if (crIndex > -1 && lfIndex > -1) {
    			if (crIndex == lfIndex + 1) {
    				return $"{LF}{CR}";	
    			} else if (lfIndex == crIndex + 1) {
    				return $"{CR}{LF}";
    			} else {
    				throw new ArgumentException("Input does not have a newline.");
    			}
    		} else if (crIndex > -1) {
    			return $"{CR}";
    		} else if (lfIndex > -1) {
    			return $"{LF}";
    		} else {
    			throw new ArgumentException("The text has a mixture of CR and LF.");
    		}
    	}
    }
    Note you have to use Regex.Split(), not String.Split(), because for some reason the .NET architects didn't provide a String.Split() overload that takes another string.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    Re: when split msg variable to more lines it not give me values in lines?

    Thank you very much for reply
    in code below
    foreach (var line in lines) {
    Console.WriteLine(line);
    }
    i will get all reading in line in code above inside for each
    suppose line have 3 lines in line inside foreach as below
    UserName:Ahmed
    Country:USA
    Member:Administrator
    i need to split lines as below and assign every line to variable
    msg1=ahmed
    msg2 = USA
    msg3=Administror
    so that how i split as above please if possible help me

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    Re: when split msg variable to more lines it not give me values in lines?

    my qr code fixed it have 3 lines so that how to get every line separately in variable as above

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: when split msg variable to more lines it not give me values in lines?

    Just like you would've before. Regex.Split() returns an array of strings just like String.Split(). So you can use the return value the same way as you did before.

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    Re: when split msg variable to more lines it not give me values in lines?

    can you please if i need to receive array inside for loop or for each how i do that
    how i use regex.split inside foreach or foorloop

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    Re: when split msg variable to more lines it not give me values in lines?

    Please i test code you give me above it work but until now i cannot split to lines
    in label2 inside foreach i get all reading in lines success
    i get lines in label2 as i need but cannot split them to variables msg1,msg2,msg3,msg4,label7
    but after for each i cannot split them to lines separtely
    if (e.KeyChar == 13 && _barcode.Count > 0)
    {

    string msg = new String(_barcode.ToArray());
    label1.Text = msg;

    string newlineToken = DetectNewline(msg);


    var lines = Regex.Split(msg, newlineToken);
    foreach(var line in lines)
    {
    label2.Text = line;
    }
    if (label2.Text.Length > 5)
    {
    string msg1 = lines[1].Substring(lines[1].IndexOf(":") + 1);
    label3.Text = msg1;
    string msg2 = lines[2].Substring(lines[2].IndexOf(":") + 1);
    label4.Text = msg2;
    string msg3 = lines[3].Substring(lines[3].IndexOf(":") + 1);
    label5.Text = msg3;
    string msg4 = lines[4].Substring(lines[4].IndexOf(":") + 1);
    label6.Text = msg4;
    label7.Text = lines[5].Substring(lines[5].IndexOf(":") + 1);
    }

    }

  8. #8
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: when split msg variable to more lines it not give me values in lines?

    Well, think about what your code is doing. It's a little hard to interpret what's going wrong, but look at this:
    Code:
    foreach(var line in lines)
    {
        label2.Text = line;
    }
    That's going to put every line into the label one by one, but only display the last one. If you want every line in the label, you have to append text. One way to do that might be:
    Code:
    var combined = string.Join(Environment.NewLine, lines);
    label2.Text = lines;
    Join() is the opposite of Split(), so it's handy for this case.

    The rest of the code's hard to interpret. Did you mean to start with 1? C# arrays start with 0. If the array has a length of 5, that means its valid indices would be 0, 1, 2, 3, and 4. And your condition is only looking at the length of the entire label's text, so it's probably always true in my version, but maybe always false in yours. I think a simpler version should look like:
    Code:
    if (lines.Length < 5) {
        string msg1 = lines[0];
        label3.Text = msg1;
        ...
        label7.Text = lines[5];
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width