|
-
Nov 7th, 2012, 09:01 AM
#1
Thread Starter
New Member
[RESOLVED] Get last 4 characters of string array of unknown size, error
I am trying to get only the last 4 digits of a string. Most of the strings are 4 digits long, but some are 8. I want to get the 4 rightmost digits. When I use stringname.length I get an error saying "Length cannot be less than zero.
Parameter name: length"
My following code has the user select a file, reads all the lines of the file, then splits it in my commented out format.
Code:
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
OpenFileDialog1.Title = "Please Select a File"
OpenFileDialog1.InitialDirectory = "C:temp"
OpenFileDialog1.ShowDialog()
'Add contents of file to dropdown list.
Static lines() As String = Nothing
lines = System.IO.File.ReadAllLines(OpenFileDialog1.FileName)
For i = 0 To UBound(lines)
'Split a lowercased version of the current line into two parts on ':'.
Dim elements() As String = lines(i).ToLower.Split(":")
'Populate list in the desired format '[address] [information]' with Zero fillers and last 4 characters of string.
cmbList.Items.Add(elements(0).Trim.ToString.PadLeft(4, "0"c) & " " & elements(1).Substring(elements.Length, -4).Trim.ToString.PadLeft(4, "0"c)) 'The text will be sent to the serial port as ascii
Next
cmbList.Text = cmbList.Items.Item(0) 'Set cmbList text to the first item on the list.
End Sub
Does anyone have some insight on this for me? I am a little stuck as this is my first project I have ever done using visual basic.
-
Nov 7th, 2012, 09:32 AM
#2
Re: Get last 4 characters of string array of unknown size, error
Here is a simple example which displays results to the IDE output window
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Items As String() = IO.File.ReadAllLines(IO.Path.Combine(Application.StartupPath, "TextFile3.txt"))
Dim result = (From T In Items Where T.Length >= 4 Select T.Substring(T.Length - 4)).ToList
For Each Item In result
Console.WriteLine("[{0}]", Item)
Next
End Sub
TextFile3.txt
Code:
123
1234
0
12345
12345678
Results
Code:
[1234]
[2345]
[5678]
-
Nov 7th, 2012, 10:04 AM
#3
Re: Get last 4 characters of string array of unknown size, error
Do you know the person in this thread?
http://www.vbforums.com/showthread.p...nge-error-help
The question is remarkably similar, though not quite the same, and the answer isn't the same, either.
In your case, you are doing this:
Substring(elements.Length, -4)
However, elements is the array of strings, not a string in the array, and as such it makes no sense. I suspect that you meant elements(1).Length rather than elements.Length, though you still have to watch out for the type of error in that other thread.
My usual boring signature: Nothing
 
-
Nov 7th, 2012, 11:03 AM
#4
Thread Starter
New Member
Re: Get last 4 characters of string array of unknown size, error
Shaggy Hiker,
No I do not, I don't see the relevance besides using a ":" separator reading from a file... And for my application I do not have to worry about that error. But thank you for that last input.
kevininstructor,
Thank you for that hint, I am trying to apply it right now.
-
Nov 7th, 2012, 11:13 AM
#5
Thread Starter
New Member
Re: Get last 4 characters of string array of unknown size, error
I think I have to add some more information.
I get an error that .length is not a member of 'char'
My information from the file is as follows for example:
100E: FFFFFFFF
1020: FFD8
when I get the information and put it to the combobox it shows up as follows:
100e ffffffff
1020 ffd8
I would like it to show up as follows:
100e ffff
1020 ffd8
-
Nov 7th, 2012, 11:35 AM
#6
Hyperactive Member
Re: Get last 4 characters of string array of unknown size, error
What's the length of a character? A char does not have a .Length property...
elements[1].Remove(0,elements[1].Length-4); //Remove all characters of element[1] from index0 to element[1]'s length, except the last 4 indexes
-
Nov 7th, 2012, 11:36 AM
#7
Re: Get last 4 characters of string array of unknown size, error
 Originally Posted by thelisa
I think I have to add some more information.
I get an error that .length is not a member of 'char'
My information from the file is as follows for example:
100E: FFFFFFFF
1020: FFD8
when I get the information and put it to the combobox it shows up as follows:
100e ffffffff
1020 ffd8
I would like it to show up as follows:
100e ffff
1020 ffd8
Try this
Code:
Dim LinesFromFile As String() = System.IO.File.ReadAllLines(OpenFileDialog1.FileName)
For Each line In LinesFromFile
If line.Length >= 10 Then
ComboBox1.Items.Add(line.Substring(0, 10).Replace(":", ""))
End If
Next
-
Nov 8th, 2012, 10:39 AM
#8
Thread Starter
New Member
Re: Get last 4 characters of string array of unknown size, error
I ended up using a combination from detlion1643 and kevininstructor and it works perfectly. Thank you for your help! I put my code snippet below in case anyone wanted to see how it ended up.
Code:
Dim elements() As String = lines(i).ToLower.Split(":")
If elements(1).Length > 4 Then elements(1) = elements(1).Remove(0, elements(1).Length - 4)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|