|
-
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.
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
|