|
-
May 20th, 2012, 10:47 PM
#1
Thread Starter
Lively Member
[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.
-
May 21st, 2012, 12:29 AM
#2
Thread Starter
Lively Member
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:
Public Sub testcode() Dim strMyString, strChar1, strChar2, strChar3, strChar4, strChar5, strChar6, strChar7, strChar8, strChar9, strChar10 As String strMyString = "Array(,,O,R,,)" 'final result will look like this "Array("Oval","Rec")" strChar1 = Switch(Mid(strMyString, 7, 1) <> ",", Mid(strMyString, 7, 1), Mid(strMyString, 7, 1) = "," And _ Mid(strMyString, 6, 1) = "(", """") strChar2 = Switch(Mid(strMyString, 8, 1) <> ",", Mid(strMyString, 8, 1), Mid(strMyString, 8, 1) = "," And _ Mid(strMyString, 7, 1) = "(", """", Mid(strMyString, 8, 1) = "," And Mid(strMyString, 7, 1) = """", "", _ Mid(strMyString, 8, 1) = "," And Mid(strMyString, 7, 1) <> """" And Mid(strMyString, 7, 1) <> "(" And _ Mid(strMyString, 9, 1) <> ")" And Mid(strMyString, 9, 1) <> ",", """,""", Mid(strMyString, 8, 1) = "," And _ Mid(strMyString, 9, 1) = "," And Mid(strMyString, 7, 1) <> ",", "", Mid(strMyString, 8, 1) = "," And Mid(strMyString, 9, 1) = ")", """") strChar3 = Switch(Mid(strMyString, 9, 1) <> ",", Mid(strMyString, 9, 1), Mid(strMyString, 9, 1) = "," And _ Mid(strMyString, 8, 1) = "(", """", Mid(strMyString, 9, 1) = "," And Mid(strMyString, 8, 1) = """", "", _ Mid(strMyString, 9, 1) = "," And Mid(strMyString, 8, 1) <> """" And Mid(strMyString, 8, 1) <> "(" And _ Mid(strMyString, 10, 1) <> ")" And Mid(strMyString, 10, 1) <> ",", """,""", Mid(strMyString, 9, 1) = "," And _ Mid(strMyString, 10, 1) = "," And Mid(strMyString, 8, 1) <> ",", "", Mid(strMyString, 9, 1) = "," And Mid(strMyString, 10, 1) = ")", """") strChar4 = Switch(Mid(strMyString, 10, 1) <> ",", Mid(strMyString, 10, 1), Mid(strMyString, 10, 1) = "," And _ Mid(strMyString, 9, 1) = "(", """", Mid(strMyString, 10, 1) = "," And Mid(strMyString, 9, 1) = """", "", _ Mid(strMyString, 10, 1) = "," And Mid(strMyString, 9, 1) <> """" And Mid(strMyString, 9, 1) <> "(" And _ Mid(strMyString, 11, 1) <> ")" And Mid(strMyString, 11, 1) <> ",", """,""", Mid(strMyString, 10, 1) = "," And _ Mid(strMyString, 11, 1) = "," And Mid(strMyString, 9, 1) <> ",", "", Mid(strMyString, 10, 1) = "," And Mid(strMyString, 11, 1) = ")", """") strChar5 = Switch(Mid(strMyString, 11, 1) <> ",", Mid(strMyString, 11, 1), Mid(strMyString, 11, 1) = "," And _ Mid(strMyString, 10, 1) = "(", """", Mid(strMyString, 11, 1) = "," And Mid(strMyString, 10, 1) = """", "", _ Mid(strMyString, 11, 1) = "," And Mid(strMyString, 10, 1) <> """" And Mid(strMyString, 10, 1) <> "(" And _ Mid(strMyString, 13, 1) <> ")" And Mid(strMyString, 13, 1) <> ",", """,""", Mid(strMyString, 11, 1) = "," And _ Mid(strMyString, 13, 1) = "," And Mid(strMyString, 10, 1) <> ",", "", Mid(strMyString, 11, 1) = "," And Mid(strMyString, 13, 1) = ")", """") strChar6 = Switch(Mid(strMyString, 12, 1) <> ",", Mid(strMyString, 12, 1), Mid(strMyString, 12, 1) = "," And _ Mid(strMyString, 11, 1) = "(", """", Mid(strMyString, 12, 1) = "," And Mid(strMyString, 11, 1) = """", "", _ Mid(strMyString, 12, 1) = "," And Mid(strMyString, 11, 1) <> """" And Mid(strMyString, 11, 1) <> "(" And _ Mid(strMyString, 13, 1) <> ")" And Mid(strMyString, 13, 1) <> ",", """,""", Mid(strMyString, 12, 1) = "," And _ Mid(strMyString, 13, 1) = "," And Mid(strMyString, 11, 1) <> ",", "", Mid(strMyString, 12, 1) = "," And Mid(strMyString, 13, 1) = ")", """") strChar7 = Switch(Mid(strMyString, 13, 1) <> ",", Mid(strMyString, 13, 1), Mid(strMyString, 13, 1) = "," And _ Mid(strMyString, 12, 1) = "(", """", Mid(strMyString, 13, 1) = "," And Mid(strMyString, 12, 1) = """", "", _ Mid(strMyString, 13, 1) = "," And Mid(strMyString, 12, 1) <> """" And Mid(strMyString, 12, 1) <> "(" And _ Mid(strMyString, 14, 1) <> ")" And Mid(strMyString, 14, 1) <> ",", """,""", Mid(strMyString, 13, 1) = "," And _ Mid(strMyString, 14, 1) = "," And Mid(strMyString, 12, 1) <> ",", "", Mid(strMyString, 13, 1) = "," And Mid(strMyString, 14, 1) = ")", """") MsgBox "Array(" & strChar1 & strChar2 & strChar3 & strChar4 & strChar5 & strChar6 & strChar7 & ")" 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
-
May 21st, 2012, 04:19 AM
#3
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 & ")"
-
May 21st, 2012, 05:04 AM
#4
Re: String Replacement ?
you can try this to see if it will work for you
vb Code:
s = "Array(,Rectangle,Oval,Circle,,)" s = Replace(Replace(Replace(Replace(Replace(s, ",,", ""), "(,", "("), "(", "("""), ")", """)"), ",", """,""") 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
-
May 21st, 2012, 02:10 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|