Group,

I've learned to find the index number of a specific character within a string by writing

dIndex = myText.IndexOf("|")

Obviously, this start counting left to right. But I'd like to do the same but start counting from the right side of the string to the left to find the character. Can this be done in VB?

If not, here's my delima:

I'm using a ComboBox that will house a list of printers to choose from. This will be populated from info in a data table. In the string of information in the ComboBox, I've put a "|" to indicate that the next character(s) will be the PrinterID (key) from the data table. Preceeding this character are multiple spaces so these characters won't be seen when the user uses the ComboBox. Since I don't know if the PrinterID will be '1' or '10' or '100', I've got to parse this number out.

In thinking out loud, if I can find the length of the string (lets call this 'sLength') and then the number of characters to the "|" (dIndex), I can do simple subtraction to find the number of "right" characters.

So, is there a "length" command to find the total number of characters in the string?

In advance, thanks.

Don

ddanbe,

In searching further, I did find 'Length', which in reading suggest that it will count the total number of characters in a string. I'm I correct on this?

Thanks for your help. Reading through this helped me make sense of many of these options.

Don

dIndex = myText.IndexOf("|")
...
So, is there a "length" command to find the total number of characters in the string?

To get total character/length :

sLength = myText.Length

But if you want to get string after | sign then you can use substring function :

PrinterId = myText.Substring(myText.IndexOf("|") + 1)

Edited 3 Years Ago by Jx_Man

Comments
+1

You could try something like this:

    PrinterId = myText.Split("|").Last

PrinterId should contain everything in your string from the last | to the end, not including the '|'.

If you need something a little more generic, here's a simple little function that will return the number of characters from either the right or the left, in a string:

Private Function RightLeft(ByVal Input As String, ByVal Count As Integer, ByVal Right As Boolean) As String
    If Right Then
        RightLeft = Input.Substring(Input.Length - Count)
    Else
        RightLeft = Input.Substring(0, Count)
    End If
End Function

Edited 3 Years Ago by tinstaafl

Jx_Man and tinstaafl,

If you saw what convoluted stuff I was writting to parse the printerID out, you'd laugh. However both of your suggestions will work much better than what stuff I wrote. Thanks for these. I may end up using both.

Thanks,

Don

tinstaafl,

I'm trying to use your suggestion, PrinterId = sitem.Split("|").Last. However I'm getting a "Object reference not set to an instance of an object" error. I'm assuming that it is because 'sitem' is defined as an object and not a string. The overall code for this event is

 Private Sub cmbxPrinter_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbxPrinter.SelectedIndexChanged
        If cmbxPrinter.SelectedIndex > -1 Then
            Dim sindex As Integer
            Dim sitem As Object
            sindex = cmbxPrinter.SelectedIndex
            sitem = cmbxPrinter.SelectedItem
            txbxCustomerNo.Focus()
        End If

        printID = sitem.Split("|").Last

Interesting enough though, I can see that 'printID' is holding the correct ID number. Can you offer a suggestion on how to fix this.

Again, thanks for the help.

Don

tinstaafl,

Never mind. I found a fix. It turned out pretty simple. For the benefit of others, here's what I did:

Private Sub cmbxPrinter_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbxPrinter.SelectedIndexChanged
        Dim sindex As Integer
        Dim sitem As Object
        Dim strsitem As String
        Dim spltitem As String
        If cmbxPrinter.SelectedIndex > -1 Then
            sindex = cmbxPrinter.SelectedIndex
            sitem = cmbxPrinter.SelectedItem
            txbxCustomerNo.Focus()
        End If
        strsitem = Convert.ToString(sitem)
        spltitem = strsitem.Split("|").Last
        printID = spltitem

And, by the way, printID wasn't holding the correct ID as I thought. It was holding the last ID number from the Data Table that was accessed earlier in the module. It just happened that I chose that printer as my test. However with the new code I've written, the correct ID is captured.

Thanks for everyones help!!

Don

Comments
Great :)

Jx_Man,

It should go without say, but I'll say it anyway: I couldn't have found my solution without your help. I greatly appreciate you and tinstaafl stepping up to help. It's one more bit of VB that I've learned!

Thanks,

Don

This question has already been answered. Start a new discussion instead.