user13316
user13316

Reputation: 35

C# String.SubString With MessageBox Does Not Display Anything

I am trying to display part of a string using a MessageBox, for this I use the String.SubString method. However when I run the code the MessageBox is not displayed and no error is thrown.

For troubleshooting purposes I display the entire string in a MessageBox before trying to display the substring.

This displays the following (Received |<BID>22|):

I want to display the number part of the string, however when I try doing this nothing is displayed. Can anyone see what is going wrong please? Here is the code:

public void parseMessage(string theMessage)
{
    String message = theMessage.Replace("\n", String.Empty);

    MessageBox.Show("Received |" + message + "|");

    String zoneNumber = message.Substring(5, message.Length);

    if (message.StartsWith("<BID>"))
    {
        MessageBox.Show("Bid received for zone " + zoneNumber);
    }
}

Upvotes: 3

Views: 611

Answers (6)

sammy_winter
sammy_winter

Reputation: 139

Try this:

String bidMarker = "<BID>";
int startLoc = message.IndexOf(bid);
if (startLoc != -1)
{
    String zoneNumber = message.Substring(startLoc + bidMarker.Length).Trim();
    MessageBox.Show("Bid received for zone " + zoneNumber);
}

Upvotes: 0

D Stanley
D Stanley

Reputation: 152566

I can't access your linked image, so I don't know for certain what message contains, but

String zoneNumber = message.Substring(5, message.Length);

should throw an exception as it would overflow the length of the string by 5 characters.

Use

String zoneNumber = message.Substring(5);

instead.

Upvotes: 5

Belial09
Belial09

Reputation: 694

so the problem is that if (message.StartsWith("<BID>")) does not return true?

does this help?

public void parseMessage(string theMessage)
{
       String message = theMessage.Replace("\r", String.Empty).Replace("\n", String.Empty).Replace("\r\n", String.Empty);
       MessageBox.Show("Received |" + message + "|");
       String zoneNumber = message.Substring(5, message.Length);
       if (message.TrimStart().StartsWith("<BID>"))
       {
          MessageBox.Show("Bid received for zone " + zoneNumber);
       }
}

Upvotes: 1

sa_ddam213
sa_ddam213

Reputation: 43596

You could use rplace instead of SubString

if (message.StartsWith("<BID>"))
{
    MessageBox.Show("Bid received for zone " + message.Replace("<BID>",""));
}

Upvotes: 0

Mathew Thompson
Mathew Thompson

Reputation: 56429

I want to display the number part of the string, however when I try doing this nothing is displayed

That's because, looking at your message, it has leading whitespace and you are trying to do StartsWith("<BID>")

First, TrimStart, then try StartsWith, or just do Contains.

StartsWith:

if (message.TrimStart().StartsWith("<BID>"))
{
    MessageBox.Show("Bid received for zone " + zoneNumber);
}

Upvotes: 1

user1088520
user1088520

Reputation:

How about changing

String zoneNumber = message.Substring(5, message.Length);

to

String zoneNumber = message.Substring(5);

Upvotes: 2

Related Questions