0

I'm reading an array of char from the plc but I have to add the character to the string if it's not empty but as in the picture all the character in the plc are empty but when run the code it add all the character anyway. I can't understand where the problem is

enter image description here

Code:

String sealNumber = "";

for (int i = 90; i < 108; i++)
{
    String c = S7.GetCharsAt(bufferLineToRead, i, 1);

    if (c != "" && c != "?")
    {
        Console.WriteLine("STRINGA " + c);
        sealNumber += c;
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • "I have to add the character to the string if it's not empty" - a *character* can't be empty. Assuming `GetCharsAt` does what I'd expect, the result would never be an empty string. Perhaps you want to check for space, or for `"\0"`? – Jon Skeet Jun 06 '22 at 05:59
  • Hi, as @Jon Skeet says, char can't be empty in OOP languages so, you need to check more specific your condition for example: what do you expect from plc which you mentioned it then check those instead. I hope it help you. – Ahmad Reza Jun 06 '22 at 07:22
  • I have check better the output of the plc and as c the output is ? what does mean this symbol? thanks – Nicola Rigoni Jun 09 '22 at 08:37

1 Answers1

1

A string can be "empty" (means no characters are in it), but a character can never be "empty". A character is always exactly one character.

That means your comparison of c != "" is most likely being true because the string has exactly one character in it.

I have no idea what the plc sends there. Maybe it's zero characters (characters with the value 0, as opposed to the UNICODE numbers that make readable letters).

You may have better luck with using !string.IsNullOrWhiteSpace(c) because that will check for all characters that aren't considered "something".

But in the end, you will need to fire up your debugger and find out what exactly happens there. We cannot do that remotely.

nvoigt
  • 75,013
  • 26
  • 93
  • 142