2014-01-26 23 views
0

我收到此错误,我似乎无法得到原因。“当我尝试创建子字符串时,索引和长度必须指向字符串中的某个位置

private void MessageReceived(String message) 
    { 
     int beginIndex = message.IndexOf(":"); 
     String index = message.Substring(1, beginIndex - 1); 
     if (index == "SHOW_TIME") 
     { 
      String time = message.Substring(11, message.Length -1); 
      if (alarm == time) 
      { 
       MessageBox.Show("ALARM ALARM ALARM ALARM"); 
      } 
     } 
    } 

当程序到达字符串时间行时,错误会弹出。

+2

假设您的字符串(消息)不包含':'或者其长度小于11 –

+0

消息字符串包含:#SHOW_TIME:XXXX% – user3236510

回答

2

您从索引11开始,但尝试去message.Length - 1。这会让你在导致错误的数组结尾之外。你需要去message.Length - 12;

String time = message.Substring(11, message.Length - 12); 
+0

谢谢!这个为我工作。 – user3236510

相关问题