473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need help with LastIndexOf

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
3 3493
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
In test() method: var path="C:\test\hello.txt"; //returns -1 for path.lastIndexOf("\\"). why?? var pos=path.lastIndexOf("\\"); //return -1 But in showFile() method: We are able to get the...
2
by: Ivar | last post by:
Hi, string s = "aXXa"; Console.WriteLine(s.LastIndexOf("XX",0)); Console.WriteLine(s.LastIndexOf("XX")); Console.WriteLine(s.IndexOf("XX",0)); Console.WriteLine(s.IndexOf("XX")); Result:...
9
by: sklett | last post by:
string url = http://localhost/subPath/Default.aspx; k = url.LastIndexOf("/", 0, url.Length); This throws an exception: Count must be positive and count must refer to a location within the...
3
by: =?Utf-8?B?dmluYXk=?= | last post by:
I am trying to check LastIndexOf "\" in a File Path, if it not there i want to append a "\" for some reason it throws error? int index = filePath.LastIndexOf("\"); Any suggestions... --
3
by: markus.rietzler | last post by:
i want to do (multiple) file upload(s) and display a progress bar. with firefox and safari it is no problem at all. only IE makes some problems. my script is based on ajax-uploader, which can be...
9
by: senfo | last post by:
I realize it's Friday and I'm probably already on vacation for the remainder of the day; but, I have a really, really stupid question. Is there a bug in the .NET 2.0 Framework in regards to the...
3
by: Mathias Weyel | last post by:
Hi Group! I encounter a strange behaviour of String.LastIndexOf when specifying start and count values. The following works: string tempString = orgString.SubString(0, mycount); int index...
2
by: cmb3587 | last post by:
I am having a problem with the validation of the account number and password. The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.