Need help with LastIndexOf

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

I have a string that is approximately 600 characters. I need to divide up
the string into 400 character chunks. I'm having trouble with using
LastIndexOf. The following code returns a value of 594.

value=msg(i).LastIndexOf(" "c)

The following code returns a value of -1

value=msg(i).LastIndexOf(" "c,1)

The following code returns an exception saying that the count parameter is
out of range.

value=msg(i).LastIndexOf(" "c,1,399)

I need help! Thanks.
 
Well, first of all, what does knowing the LastIndexOf have to do with
dividing the String into 400 character chunks? Second, I want to make sure
you know that the first index of a String is 0, not 1 (I believe this is a
change from VB6, but most languages do use indexes starting at 0, so you
should get used to it). Third, where is the value for i coming from? If this
code is inside a loop, please show us the loop code and any other code
inside the loop so we know everything that is going on, I think that might
have something to do with the error you are getting in your last piece of
code you show here. Also, are the answers you are recieving from the first
two pieces of code the ones you expected? If not, what did you expect and
what was the value of msg(i) when you executed those pieces of code? Please
give more info, I can't really help you until you do. Thanks.
 
Terry said:
I have a string that is approximately 600 characters. I need to divide up
the string into 400 character chunks. I'm having trouble with using
LastIndexOf. The following code returns a value of 594.

value=msg(i).LastIndexOf(" "c)

The following code returns a value of -1

value=msg(i).LastIndexOf(" "c,1)

The following code returns an exception saying that the count parameter is
out of range.

value=msg(i).LastIndexOf(" "c,1,399)

I need help! Thanks.

Sry I'm a bit confused on your issue. Are you saying you want to find
the last space of the string that is less than 400 charcters long?

If so try this:

if msg(i).length > 400 then
dim FirstBlankUnder400 as integer
FirstBlankUnder400 = msg(i).substring(0,400).LastIndexof(" ")
Dim StringUnder400 as string
StringUnder400 = msg(i).substring(0,FirstBlankUnder400)
end if

Chris
 
This actually works. I guess that it counts from the end backwards. I'm
splitting up the text into 400 character chunks (breaking at a space so as
not to cut a word in half).

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click
'Remove ending Carriage Returns and/or Line Feeds
While txtMsg.Text.EndsWith(vbCr) Or txtMsg.Text.EndsWith(vbLf)
If txtMsg.Text.EndsWith(vbLf) = True Then txtMsg.Text =
txtMsg.Text.Remove(txtMsg.Text.Length - 1, 1)
If txtMsg.Text.EndsWith(vbCr) = True Then txtMsg.Text =
txtMsg.Text.Remove(txtMsg.Text.Length - 1, 1)
End While
'Split the text into chunks separated by Carriage Returns
Dim msg() As String = Split(txtMsg.Text.Replace(vbCrLf, vbCr), vbCr)
For i As Integer = 0 To msg.Length - 1
If msg(i).Length <= 399 Then
SendToHost(msg(i) & vbCr)
Else
While msg(i).Length > 399
Dim at As Integer = msg(i).LastIndexOf(" ", 399, 399)
Dim tmp As String = Trim(msg(i).Substring(0, at))
SendToHost(tmp & vbCr)
msg(i) = Trim(msg(i).Substring(at))
End While
If msg(i) <> "" Then SendToHost(msg(i) & vbCr)
End If
Next
End Sub
 
Back
Top