我有一條消息,它說287個字符長。我需要在160個字符之後將它分成兩部分,但我的代碼仍然無法工作。我搜索了很多,並嘗試了很多不同的解決方案,但沒有像我所期望的那樣工作。在我的腦海中,這是一個簡單的解決方案,但在實踐中,它使我做了噩夢!使用子串超出範圍拆分字符串
// a check is done to ensure the message is > 160 in length.
string _message;
_message = "this is my long message which needs to be split in to two string after 160 characters. This is a long message. This is a long message. This is a long message. This is a long message. This is a long message.";
string message1 = _message.Substring(0,160);
string message2 = _message.Substring(161,_message.Length);
上面根本不起作用 - 雖然給我第二個子字符串的異常錯誤。
任何人都可以幫忙嗎?該消息永遠不會超過320個字符。
-1當你先拿下160個物品,項目,你的開始索引是160不是161. –
@SaeedAmiri:更正,謝謝。 –
和downvote刪除 –