Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

concatenate String VB? and deconcatenate

enkii82enkii82 Posts: 14Member
help me please!!
tell me how to concatenate numbers into a string in VB? and how to separate back the string into separated numbers?

and how to specify the no. of item of the combolist to be called to get the list item ?
show the the code and if possible some example, please.

Comments

  • GenjuroGenjuro Posts: 913Member
    : help me please!!
    : tell me how to concatenate numbers into a string in VB? and how to separate back the string into separated numbers?
    :
    : and how to specify the no. of item of the combolist to be called to get the list item ?
    : show the the code and if possible some example, please.
    :

    Concatenating string and numbers in VB it's so easy that it's embarassing, compared to other languages:

    [code]
    sString as string
    iNumber as integer
    sResult as string

    sResult = sString & iNumber
    [/code]
    To concatenate two numbers "as strings", you can use the same "&" operator, but keep in mind that concatenating "123" and "456" will return a string with the value "123456", which you have no way to separate later.

    As to separate the string, well... this isn't that embarassing and really depends on the format of the string.
    You have to write a function yourself, but you can use Mid, Instr, InStrRev, Left, Right, and Split on your side.
    You can put a "separator" of sort, however, into the string:
    [code]
    Dim iPipePos as integer
    sResult = sString & "|" & iNumber
    [/code]
    Then, it's easy to find it and know that the piece on the left of the separator is the string, and the other piece is the number; just find the position of the separator character with Instr (maybe InStrRev, just in case), and then use Mid to extract the left piece and right piece.

    As for combos:
    [code]
    dim SelectedItem as string
    SelectedItem = cboBox.List(cboBox.ListIndex)
    [/code]
    is enough to get the text (.List) of the currently selected item (.ListIndex). Just keep in mind that you need to check that ListIndex isn't -1 before that call, or it will fail.
  • enkii82enkii82 Posts: 14Member
    thanks for your answer. but my prob with the separator is that how can i separate like ... 12|3|4|67|9|14 .. a string of numbers into array of integer? and with unknown length of numbers in the string into known count of integer array? if yes, can we choose not to initialize the length of the array integer and let the count do the work?? like C++?? using malloc??

    haha.. u got me, man. im not really a good user in VB 6.0.
  • dokken2dokken2 Posts: 532Member
    : help me please!!
    : tell me how to concatenate numbers into a string in VB? and how to separate back the string into separated numbers?
    :
    : and how to specify the no. of item of the combolist to be called to get the list item ?
    : show the the code and if possible some example, please.
    :

    [code]

    'parse string of numbers into integers
    Private Sub Command1_Click()

    'each number must be followed by separator char
    Const MyString = "1|16|128|1024|"
    Dim tmp As String
    Dim char As String * 1
    Dim x As Integer

    For x = 1 To Len(MyString)
    'parse one char
    char = Mid(MyString, x, 1)
    If char <> "|" Then
    'concat next char
    tmp = tmp & char
    Else
    'convert string to integer
    MsgBox CInt(tmp)
    'set tmp to empty string
    tmp = ""
    End If
    Next

    End Sub

    [/code]
Sign In or Register to comment.