VB Logic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kirubagari
    New Member
    • Jun 2007
    • 158

    VB Logic

    Need some help

    I have 2 string

    String 1: A-B-C-D

    String 2: A-B-C

    If the string have 4 token

    i need take A and concenate with D

    A-D

    if i have 3 token like A-B-C

    i need take the 1st token A

    Please help on this.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    kirubagari,

    You've made absolutly no sense to me and I'd like to believe that I am a reasonably intelligent person...

    So, do yo want to try that again?

    Comment

    • kirubagari
      New Member
      • Jun 2007
      • 158

      #3
      Hi zmbd ,
      Let me explain again.
      I have 2 string.
      Code:
      TFSGNT-SAH-CMP-PreMeas
      ILD-SAH-CMP-Meas-5P25K
      If the string have 3 dash in between, i need to grab the 1st value before the first dash
      Code:
      TFSGNT-SAH-CMP-PreMeas
      Code:
      TFSGNT
      Code:
      ILD-SAH-CMP-Meas-5P25K
      If the string have 4 dash in between, i need to grab first string and Concatenate
      with last string as below
      Code:
      ILD-5P25K

      Comment

      • aliyalcin
        New Member
        • Sep 2012
        • 2

        #4
        Hi kirubagari,
        You can use the following function.

        Code:
            Public Function stringProcessor(ByVal inputString As String) As String
                Dim result As String = String.Empty
                Dim stringList As List(Of String) = inputString.Split("-"c).ToList()
                If stringList.Count > 0 AndAlso stringList.Count = 5 Then
                    result = stringList(0) & "-" & stringList(stringList.Count-1)
                ElseIf stringList.Count > 0 AndAlso stringList.Count = 4 Then
                    result = stringList(0)
                End If
                Return result
            End Function

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          There are a number of ways to solve this:
          Split function comes to mind first http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx. With three hyphens in string you will never return more than four sections so check the return for item count.
          Second is the recursive instr to return positions.
          Third is a midstring,
          and so on with the string functions.http://msdn.microsoft.com/en-us/library/dd789093.aspx... I like the filter; however, I'm old school and arrays were the bread and butter of programming.

          Comment

          • LeoT
            New Member
            • Sep 2012
            • 1

            #6
            Code:
            Public Function FinalStr() As String
                Dim a As String
                Dim b As String
                a = "TFSGNT-SAH-CMP-PreMeas"
                b = "ILD-SAH-CMP-Meas-5P25K"
                
                Dim ar
                Dim br
                Dim intcnt As Integer
                Dim firstStr As String
                Dim secondStr As String
                
                ar = Split(a, "-")
                If UBound(ar) = 3 Then
                    firstStr = ar(0)
                End If
                
                br = Split(b, "-")
                If UBound(br) = 4 Then
                    secondStr = br(4)
                End If
                'MsgBox firstStr & "-" & secondStr
                
                FinalStr = firstStr & "-" & secondStr
                
            End Function
            Last edited by zmbd; Sep 14 '12, 04:37 AM. Reason: LeoT, Please use the code tags when posting code (button on the bar)/ ALso you need to give an explanation behind the posted code. thnx

            Comment

            • kirubagari
              New Member
              • Jun 2007
              • 158

              #7
              Hi Experts ,

              Thanks.Able to solve the problem
              Code:
              Private Sub Command1_Click()
              Dim a As String
              Dim b As String
              ' = "TFSGNT-SAH-CMP-PreMeas"
              a = "ILD-SAH-CMP-Meas-5P25K"
              
              Dim ar
              'Dim br
              Dim intcnt As Integer
              Dim firstStr As String
              Dim secondStr As String
              Dim abc As String
              
              ar = Split(a, "-")
              If UBound(ar) = 3 Then
              FinalStr = ar(0)
              ElseIf UBound(ar) = 4 Then
              secondStr = ar(4)
              firstStr = ar(0)
              abc = firstStr & "-" & secondStr
              End If
              
              End Sub

              Comment

              Working...