简体   繁体   English

带有MessageBox的C#String.SubString不显示任何内容

[英]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. 我试图使用MessageBox显示string一部分,为此我使用String.SubString方法。 However when I run the code the MessageBox is not displayed and no error is thrown. 但是,当我运行代码时,不显示MessageBox并且不会引发错误。

For troubleshooting purposes I display the entire string in a MessageBox before trying to display the substring . 出于疑难解答的目的,我在尝试显示substring之前在MessageBox中显示整个substring

This displays the following ( Received |<BID>22| ): 这将显示以下内容(已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);
    }
}

I can't access your linked image, so I don't know for certain what message contains, but 我无法访问您链接的图片,所以我不知道哪些message包含,但是

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

should throw an exception as it would overflow the length of the string by 5 characters. 应抛出异常,因为它会使字符串的长度溢出5个字符。

Use 采用

String zoneNumber = message.Substring(5);

instead. 代替。

How about changing 如何改变

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

to

String zoneNumber = message.Substring(5);

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>") 那是因为,看着你的消息, 它有领先的空白 ,你正在尝试做StartsWith("<BID>")

First, TrimStart , then try StartsWith , or just do Contains . 首先, TrimStart ,然后尝试StartsWith ,或者只做Contains

StartsWith : StartsWith

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

so the problem is that if (message.StartsWith("<BID>")) does not return true? 所以问题是if (message.StartsWith("<BID>"))不返回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);
       }
}

You could use rplace instead of SubString 您可以使用rplace而不是SubString

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

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM