这似乎运作良好 . 我测试了它的消息长度为0,3,160,320,323个字符 . 有更短的写作方式,但我发布这个(一些)清晰度 .
Private Sub SendMessage(message As String)
If message.Length > 0 Then
Dim multiPartMessages As New List(Of String)
'get the number of parts that are 160 characters in length
Dim fullParts As Integer = CInt(message.Length / 160)
'calculate the remaining number of characters
Dim lastPartLength = message.Length - (fullParts * 160)
'if the full message>160 characters in length loop through the string
'in steps of i*160 and add each substring to multiPartMessages
Dim i As Integer
While i < fullParts And fullParts > 0
multiPartMessages.Add(message.Substring(i * 160, 160))
i += 1
End While
'if the last part has more than 0 characters then add them all to multiPartMessages
If lastPartLength > 0 Then
multiPartMessages.Add(message.Substring(fullParts * 160, lastPartLength))
End If
Else
'handle 0 length message here
End If
End Sub