String manipulation question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    #1

    String manipulation question

    Hi,

    What would be the best way to remove the following from the start of a
    string...

    "A1", "A2, "A3", "A4", "A5", "B1, "B2", "B3", "B4", "B5", "C1", "C2", "C3",
    "C4", "C5", "D1","D2","D3", "D4","D5".

    I hope I explained that right. A string will have one of the above strings
    as its first two characters and I need them removing from the string.

    I tried this...
    string1 = replace(string1 ,"A1","")
    string1 = replace(string1 ,"A2","")
    string1 = replace(string1 ,"A3","")
    etc..

    and although it works its not very efficient as I would need to do this for
    all the above strings!

    Cheers,
    Paul


    Seth Rowe are you out there?!


  • Dennis

    #2
    RE: String manipulation question

    Select case string1.substri ng(0,2)
    case "A1", "A2, "A3", "A4", "A5", "B1, "B2", "B3", "B4", "B5", "C1",
    "C2", "C3",
    "C4", "C5", "D1","D2","D3", "D4","D5"
    String1 = string1.substri ng(2,string1.le ngth-2)
    case else
    end select


    --
    Dennis in Houston


    "Paul" wrote:
    Hi,
    >
    What would be the best way to remove the following from the start of a
    string...
    >
    "A1", "A2, "A3", "A4", "A5", "B1, "B2", "B3", "B4", "B5", "C1", "C2", "C3",
    "C4", "C5", "D1","D2","D3", "D4","D5".
    >
    I hope I explained that right. A string will have one of the above strings
    as its first two characters and I need them removing from the string.
    >
    I tried this...
    string1 = replace(string1 ,"A1","")
    string1 = replace(string1 ,"A2","")
    string1 = replace(string1 ,"A3","")
    etc..
    >
    and although it works its not very efficient as I would need to do this for
    all the above strings!
    >
    Cheers,
    Paul
    >
    >
    Seth Rowe are you out there?!
    >
    >
    >

    Comment

    • _AnonCoward

      #3
      Re: String manipulation question


      "Paul" <iam@home.co.uk wrote in message
      news:IemdnfNoXM 3Uk8jYRVnysw@pi pex.net...
      : Hi,
      :
      : What would be the best way to remove the following from the start of
      : a string...
      :
      : "A1", "A2, "A3", "A4", "A5", "B1, "B2", "B3", "B4", "B5", "C1",
      : "C2", "C3", "C4", "C5", "D1","D2","D3", "D4","D5".
      :
      : I hope I explained that right. A string will have one of the above
      : strings as its first two characters and I need them removing from
      the
      : string.
      :
      : I tried this...
      : string1 = replace(string1 ,"A1","")
      : string1 = replace(string1 ,"A2","")
      : string1 = replace(string1 ,"A3","")
      : etc..
      :
      : and although it works its not very efficient as I would need to do
      : this for all the above strings!
      :
      : Cheers,
      : Paul


      Several approaches come to mind:


      If the strings will *always* start with those values, just strip off
      everything that follows:

      Newvalue = Mid(OldValue, 3)


      If the strings may or may not start with those values, then:

      Select Case Left(OldValue, 2)
      Case "A1", "A2", "A3", "A4", "A5", _
      "B1", "B2", "B3", "B4", "B5", _
      "C1", "C2", "C3", "C4", "C5", _
      "D1", "D2", "C3", "D4", "D5", _

      NewValue = Mid(OldValue, 3)

      Case Else

      NewValue = OldValue

      End Select


      Or

      If Len(OldValue) < 2 Then
      NewValue = OldValue
      ElseIf Mid(OldValue, 1, 1) 1 >= "A" AndAlso _
      Mid(OldValue, 1, 1) <= "E" Then

      If Mid(OldValue, 2, 1) >= "1" AndAlso _
      Mid(OldValue, 2, 1) <= "5" Then

      NewValue = Mid(OldValue, 3)
      Else
      NewValue = OldValue
      End If
      Else
      NewValue = OldValue
      End If


      Or best of all, use a regular expression:

      Imports System.Text.Reg ularExpressions

      [...]

      Dim Pattern as string = "^[A-Ea-e][1-5].*$"

      If Regex.isMatch(O ldValue, Pattern) Then
      NewValue = Mid(OldValue, 3)
      Else
      NewValue = OldValue
      End If

      Ralf
      --
      --
      ----------------------------------------------------------
      * ^~^ ^~^ *
      * _ {~ ~} {~ ~} _ *
      * /_``>*< >*<''_\ *
      * (\--_)++) (++(_--/) *
      ----------------------------------------------------------
      There are no advanced students in Aikido - there are only
      competent beginners. There are no advanced techniques -
      only the correct application of basic principles.


      Comment

      • Paul

        #4
        Re: String manipulation question

        Fantastic!
        Thanks Dennis and Ralf.

        "_AnonCowar d" <abcdef@uvwxyz. comwrote in message
        news:QL95h.3409 4$39.30528@sout heast.rr.com...
        >
        "Paul" <iam@home.co.uk wrote in message
        news:IemdnfNoXM 3Uk8jYRVnysw@pi pex.net...
        : Hi,
        :
        : What would be the best way to remove the following from the start of
        : a string...
        :
        : "A1", "A2, "A3", "A4", "A5", "B1, "B2", "B3", "B4", "B5", "C1",
        : "C2", "C3", "C4", "C5", "D1","D2","D3", "D4","D5".
        :
        : I hope I explained that right. A string will have one of the above
        : strings as its first two characters and I need them removing from
        the
        : string.
        :
        : I tried this...
        : string1 = replace(string1 ,"A1","")
        : string1 = replace(string1 ,"A2","")
        : string1 = replace(string1 ,"A3","")
        : etc..
        :
        : and although it works its not very efficient as I would need to do
        : this for all the above strings!
        :
        : Cheers,
        : Paul
        >
        >
        Several approaches come to mind:
        >
        >
        If the strings will *always* start with those values, just strip off
        everything that follows:
        >
        Newvalue = Mid(OldValue, 3)
        >
        >
        If the strings may or may not start with those values, then:
        >
        Select Case Left(OldValue, 2)
        Case "A1", "A2", "A3", "A4", "A5", _
        "B1", "B2", "B3", "B4", "B5", _
        "C1", "C2", "C3", "C4", "C5", _
        "D1", "D2", "C3", "D4", "D5", _
        >
        NewValue = Mid(OldValue, 3)
        >
        Case Else
        >
        NewValue = OldValue
        >
        End Select
        >
        >
        Or
        >
        If Len(OldValue) < 2 Then
        NewValue = OldValue
        ElseIf Mid(OldValue, 1, 1) 1 >= "A" AndAlso _
        Mid(OldValue, 1, 1) <= "E" Then
        >
        If Mid(OldValue, 2, 1) >= "1" AndAlso _
        Mid(OldValue, 2, 1) <= "5" Then
        >
        NewValue = Mid(OldValue, 3)
        Else
        NewValue = OldValue
        End If
        Else
        NewValue = OldValue
        End If
        >
        >
        Or best of all, use a regular expression:
        >
        Imports System.Text.Reg ularExpressions
        >
        [...]
        >
        Dim Pattern as string = "^[A-Ea-e][1-5].*$"
        >
        If Regex.isMatch(O ldValue, Pattern) Then
        NewValue = Mid(OldValue, 3)
        Else
        NewValue = OldValue
        End If
        >
        Ralf
        --
        --
        ----------------------------------------------------------
        * ^~^ ^~^ *
        * _ {~ ~} {~ ~} _ *
        * /_``>*< >*<''_\ *
        * (\--_)++) (++(_--/) *
        ----------------------------------------------------------
        There are no advanced students in Aikido - there are only
        competent beginners. There are no advanced techniques -
        only the correct application of basic principles.
        >
        >

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: String manipulation question

          Paul,

          You have your answer already but I would probably in this case, use a split
          command and than loop through the parts.

          Cor

          "Paul" <iam@home.co.uk schreef in bericht
          news:IemdnfNoXM 3Uk8jYRVnysw@pi pex.net...
          Hi,
          >
          What would be the best way to remove the following from the start of a
          string...
          >
          "A1", "A2, "A3", "A4", "A5", "B1, "B2", "B3", "B4", "B5", "C1", "C2",
          "C3", "C4", "C5", "D1","D2","D3", "D4","D5".
          >
          I hope I explained that right. A string will have one of the above
          strings as its first two characters and I need them removing from the
          string.
          >
          I tried this...
          string1 = replace(string1 ,"A1","")
          string1 = replace(string1 ,"A2","")
          string1 = replace(string1 ,"A3","")
          etc..
          >
          and although it works its not very efficient as I would need to do this
          for all the above strings!
          >
          Cheers,
          Paul
          >
          >
          Seth Rowe are you out there?!
          >
          >

          Comment

          • Pascal

            #6
            Re: String manipulation question

            >What would be the best way to remove the following from the start of a
            >string...
            >>
            >"A1", "A2, "A3", "A4", "A5", "B1, "B2", "B3", "B4", "B5", "C1", "C2",
            >"C3", "C4", "C5", "D1","D2","D3", "D4","D5".
            >>
            >>>>
            >I tried this...
            >string1 = replace(string1 ,"A1","")
            >string1 = replace(string1 ,"A2","")
            >string1 = replace(string1 ,"A3","")
            >etc..
            >>
            i will try a loop with string = mid$(string,3, len((string)-3))


            Comment

            Working...