-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?

Marcus
  • 1
  • 2
  • 4
    Note that Trim does not edit the string. It returns a new string that has been trimmed. – Mike Zboray Mar 02 '18 at 00:37
  • Also, you could just use `String.IsNullOrWhiteSpace`. – Shelby115 Mar 02 '18 at 00: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(). – Hans Passant Mar 02 '18 at 01: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. – Enigmativity Mar 02 '18 at 03:50
  • the marked "duplicates" didn't bring the solution, it's still not working. Thanks for trying to help me. – Marcus Mar 02 '18 at 18:52
  • What is the exact value of `value`? `value.Length`? `value.Trim().Length`? – mjwills Mar 04 '18 at 03:59
  • serverMsg.Trim() is the value – Marcus Mar 04 '18 at 10:29
  • I asked three questions. You answered none of them. The latter two questions, for example, would have a **number** as the answer. – mjwills Mar 04 '18 at 22:26
  • so i missunderstood your question, sorry. value is like: Quelle-95-94-true length: different but always Name-XPos-YPos-boolean Trim.Length should be the same as length, but if there were Spaces, it should remove them – Marcus Mar 06 '18 at 06:48

2 Answers2

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

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

Andrew
  • 720
  • 3
  • 9
  • 34
  • I tried it with IsNullOrWhitespace() and edited my Question, because it poorly didn't work for me – Marcus Mar 02 '18 at 12:00
  • @mjwills You're right, I just copied and pasted it and added the NullOrWhitesapce. I will update now – Andrew Mar 02 '18 at 16:08
-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...
  ...
)
Curt
  • 5,518
  • 1
  • 21
  • 35