By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,694 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,694 IT Pros & Developers. It's quick & easy.

Need help with LastIndexOf

P: n/a
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.
Feb 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Terry Olsen" <to******@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
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.

Feb 12 '06 #2

P: n/a
Terry Olsen wrote:
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
Feb 12 '06 #3

P: n/a
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
"Terry Olsen" <to******@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
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.

Feb 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.