Results 1 to 5 of 5

Thread: [RESOLVED] String Replacement ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Location
    VietNam
    Posts
    81

    Resolved [RESOLVED] String Replacement ?

    i have a string. let say

    strMyString = "Array(,Rectangle,Oval,Circle,,)"

    and i want my final result something like this

    strMyString = "Array("Rectangle","Oval","Circle")"

    i struggled a few days or so in VBA with many functions such as mid,instr,instrrev,loop. i know the problems are the characters before and after this mark "," (the comma ) however i'm still stuck.Can any of you can do this. Meanwhile i will do my best before check out this post again. any help is appreciated.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Location
    VietNam
    Posts
    81

    Re: String Replacement ?

    for simpler string such as strMyString = "Array(,,O,R,,)". i nearly did it with the following code just a little bit wrong logic somewhere in the first character however i think i will fine-tune it with no problem after a short sleep ( now i feel quite a lot more relax and happy with my effort so far ). if you guys have better solution just post it here for everyone reference.
    vb Code:
    1. Public Sub testcode()
    2.     Dim strMyString, strChar1, strChar2, strChar3, strChar4, strChar5, strChar6, strChar7, strChar8, strChar9, strChar10 As String
    3.     strMyString = "Array(,,O,R,,)" 'final result will look like this "Array("Oval","Rec")"
    4.    
    5.     strChar1 = Switch(Mid(strMyString, 7, 1) <> ",", Mid(strMyString, 7, 1), Mid(strMyString, 7, 1) = "," And _
    6.     Mid(strMyString, 6, 1) = "(", """")
    7.    
    8.     strChar2 = Switch(Mid(strMyString, 8, 1) <> ",", Mid(strMyString, 8, 1), Mid(strMyString, 8, 1) = "," And _
    9.     Mid(strMyString, 7, 1) = "(", """", Mid(strMyString, 8, 1) = "," And Mid(strMyString, 7, 1) = """", "", _
    10.     Mid(strMyString, 8, 1) = "," And Mid(strMyString, 7, 1) <> """" And Mid(strMyString, 7, 1) <> "(" And _
    11.     Mid(strMyString, 9, 1) <> ")" And Mid(strMyString, 9, 1) <> ",", """,""", Mid(strMyString, 8, 1) = "," And _
    12.     Mid(strMyString, 9, 1) = "," And Mid(strMyString, 7, 1) <> ",", "", Mid(strMyString, 8, 1) = "," And Mid(strMyString, 9, 1) = ")", """")
    13.    
    14.     strChar3 = Switch(Mid(strMyString, 9, 1) <> ",", Mid(strMyString, 9, 1), Mid(strMyString, 9, 1) = "," And _
    15.     Mid(strMyString, 8, 1) = "(", """", Mid(strMyString, 9, 1) = "," And Mid(strMyString, 8, 1) = """", "", _
    16.     Mid(strMyString, 9, 1) = "," And Mid(strMyString, 8, 1) <> """" And Mid(strMyString, 8, 1) <> "(" And _
    17.     Mid(strMyString, 10, 1) <> ")" And Mid(strMyString, 10, 1) <> ",", """,""", Mid(strMyString, 9, 1) = "," And _
    18.     Mid(strMyString, 10, 1) = "," And Mid(strMyString, 8, 1) <> ",", "", Mid(strMyString, 9, 1) = "," And Mid(strMyString, 10, 1) = ")", """")
    19.    
    20.     strChar4 = Switch(Mid(strMyString, 10, 1) <> ",", Mid(strMyString, 10, 1), Mid(strMyString, 10, 1) = "," And _
    21.     Mid(strMyString, 9, 1) = "(", """", Mid(strMyString, 10, 1) = "," And Mid(strMyString, 9, 1) = """", "", _
    22.     Mid(strMyString, 10, 1) = "," And Mid(strMyString, 9, 1) <> """" And Mid(strMyString, 9, 1) <> "(" And _
    23.     Mid(strMyString, 11, 1) <> ")" And Mid(strMyString, 11, 1) <> ",", """,""", Mid(strMyString, 10, 1) = "," And _
    24.     Mid(strMyString, 11, 1) = "," And Mid(strMyString, 9, 1) <> ",", "", Mid(strMyString, 10, 1) = "," And Mid(strMyString, 11, 1) = ")", """")
    25.    
    26.     strChar5 = Switch(Mid(strMyString, 11, 1) <> ",", Mid(strMyString, 11, 1), Mid(strMyString, 11, 1) = "," And _
    27.     Mid(strMyString, 10, 1) = "(", """", Mid(strMyString, 11, 1) = "," And Mid(strMyString, 10, 1) = """", "", _
    28.     Mid(strMyString, 11, 1) = "," And Mid(strMyString, 10, 1) <> """" And Mid(strMyString, 10, 1) <> "(" And _
    29.     Mid(strMyString, 13, 1) <> ")" And Mid(strMyString, 13, 1) <> ",", """,""", Mid(strMyString, 11, 1) = "," And _
    30.     Mid(strMyString, 13, 1) = "," And Mid(strMyString, 10, 1) <> ",", "", Mid(strMyString, 11, 1) = "," And Mid(strMyString, 13, 1) = ")", """")
    31.    
    32.     strChar6 = Switch(Mid(strMyString, 12, 1) <> ",", Mid(strMyString, 12, 1), Mid(strMyString, 12, 1) = "," And _
    33.     Mid(strMyString, 11, 1) = "(", """", Mid(strMyString, 12, 1) = "," And Mid(strMyString, 11, 1) = """", "", _
    34.     Mid(strMyString, 12, 1) = "," And Mid(strMyString, 11, 1) <> """" And Mid(strMyString, 11, 1) <> "(" And _
    35.     Mid(strMyString, 13, 1) <> ")" And Mid(strMyString, 13, 1) <> ",", """,""", Mid(strMyString, 12, 1) = "," And _
    36.     Mid(strMyString, 13, 1) = "," And Mid(strMyString, 11, 1) <> ",", "", Mid(strMyString, 12, 1) = "," And Mid(strMyString, 13, 1) = ")", """")
    37.    
    38.     strChar7 = Switch(Mid(strMyString, 13, 1) <> ",", Mid(strMyString, 13, 1), Mid(strMyString, 13, 1) = "," And _
    39.     Mid(strMyString, 12, 1) = "(", """", Mid(strMyString, 13, 1) = "," And Mid(strMyString, 12, 1) = """", "", _
    40.     Mid(strMyString, 13, 1) = "," And Mid(strMyString, 12, 1) <> """" And Mid(strMyString, 12, 1) <> "(" And _
    41.     Mid(strMyString, 14, 1) <> ")" And Mid(strMyString, 14, 1) <> ",", """,""", Mid(strMyString, 13, 1) = "," And _
    42.     Mid(strMyString, 14, 1) = "," And Mid(strMyString, 12, 1) <> ",", "", Mid(strMyString, 13, 1) = "," And Mid(strMyString, 14, 1) = ")", """")
    43.    
    44.     MsgBox "Array(" & strChar1 & strChar2 & strChar3 & strChar4 & strChar5 & strChar6 & strChar7 & ")"
    45. End Sub

    PS: as i said, the problems lie in the mark "," ( the comma ). the characters after it and before it and what happens after the first replacement occurs. i believe there must be some better ways beyond my knowledge at the time being and you guys the professionals can demonstrate it.if so do it please, it can be beautiful, thanks again
    Last edited by rocker123; May 21st, 2012 at 02:11 PM. Reason: PS

  3. #3
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: String Replacement ?

    try like this
    Code:
    Dim StrMyString As String, MyStr As String, StrResult As String
    Dim StrSplit, i  As Long
    
    StrMyString = "Array(,,O,R,,)"
    StrSplit = Split(StrMyString, ",")
    For i = LBound(StrSplit) + 1 To UBound(StrSplit) - 1
        If StrSplit(i) <> "" Then
            Select Case StrSplit(i)
                Case "O"
                    MyStr = "Ovel"
                Case "R"
                    MyStr = "Rec"
                'add more case as u need
            End Select
            If StrResult = "" Then
                StrResult = MyStr
            Else
                StrResult = StrResult & "," & MyStr
            End If
        End If
    Next
    StrResult = "Array(" & StrResult & ")"
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: String Replacement ?

    you can try this to see if it will work for you
    vb Code:
    1. s = "Array(,Rectangle,Oval,Circle,,)"
    2. s = Replace(Replace(Replace(Replace(Replace(s, ",,", ""), "(,", "("), "(", "("""), ")", """)"), ",", """,""")
    3. msgbox s
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Location
    VietNam
    Posts
    81

    Re: String Replacement ?

    thanks seenu_1st. i actually thought of something like you but give up because i don't know there is a function called split

    thanks westconn1 again, your code is gorgeous, simple , complicated and so attractive. i will rate your post again immediately after i gain some reputation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width