-2

my goal is to get a message from an other client over a server and work on with that message. Server and other client are in visual basic and if i try to communicate between two vb clients everything is fine, but i need the client in c# for my Unityproject. My Problem is that there are still empty Messages at the Console, so i think the if() doesn't work correct. Here is the relevant part of the Code:

try
            {
                theStream = mySocket.GetStream();
                Byte[] inStream = new Byte[mySocket.SendBufferSize];
                theStream.Read(inStream, 0, inStream.Length);
                serverMsg += Encoding.ASCII.GetString(inStream);
                serverMsg = serverMsg.Trim();

                //if ((serverMsg == null || serverMsg == "")) doesn't work
                //if (String.IsNullOrWhiteSpace(serverMsg)) doesn't work
                //if (String.IsNullOrEmpty(serverMsg) || serverMsg.Length <1) doesn't work  
                INOWS = false;
                INOWS = IsNullOrWhiteSpace(serverMsg);

                if (INOWS) 
                {
                    // do nothing
                }
                else
                {
                    Debug.Log("[SERVER] -> " + serverMsg);

                }
            }
            catch (Exception e)
            {
                Debug.Log("[Fehler]" + e);
            }
        } while (socketReady == true);
public static bool IsNullOrWhiteSpace(string value)
{
    if (value != null)
    {
        for (int i = 0; i < value.Length; i++)
        {
            if (!char.IsWhiteSpace(value[i]))
            {
                return false;
            }
        }
    }
    return true;
}

Thanks to your hints i tried using IsNullOrWhiteSpace, but this gave me an error "'string' does not contain a definition for IsNullOrWhiteSpace" So i used this IsNullOrWhitespace but i still get at least one empty string at the console for every korrekt string. console view Do you have any other hints for me?

9
  • 4
    Note that Trim does not edit the string. It returns a new string that has been trimmed. Commented Mar 2, 2018 at 0:37
  • Also, you could just use String.IsNullOrWhiteSpace. Commented Mar 2, 2018 at 0:39
  • Using SendBufferSize to read makes no sense whatsoever. 4096 is a nice round value. Trim() does not remove the zeros from the buffer, nor is it used correctly. Pay attention to the return value of Read(), it tells you how many bytes are actually in the buffer and what you need to pass to GetString(). Commented Mar 2, 2018 at 1:05
  • You should really avoid writing catch (Exception e) - it leads to swallowing errors and giving you the false sense of security that your code is bug-free. Commented Mar 2, 2018 at 3:50
  • the marked "duplicates" didn't bring the solution, it's still not working. Thanks for trying to help me. Commented Mar 2, 2018 at 18:52

2 Answers 2

1
if (String.IsNullOrWhitespace(serverMsg))                    
                {
                    // do nothing
                }

IsNullOrWhitespace() checks null, whitespace(" ") and empty("")

Sign up to request clarification or add additional context in comments.

2 Comments

I tried it with IsNullOrWhitespace() and edited my Question, because it poorly didn't work for me
@mjwills You're right, I just copied and pasted it and added the NullOrWhitesapce. I will update now
-1

Try trimming the string: if the string has any whitespace characters in it, it will still fail the IsNullOrEmpty() test.

In such cases (particularly with user or web service input), I generally use something like

if ((s ?? "").Trim() != "")
{
  //  do whatever processing you need to do here...
  ...
)

1 Comment

I guess that's what IsNullOrWhiteSpace is for! Why reinvent the wheel?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.