如果字符串格式不统一呢? 比如,是一条sql语句呢? ex:"select * from aaa where col1=col11" 要转换成"select * from aaa where 001=011" 要怎么转换??
Sub test() Dim strTmp As String Dim strNew As String Dim I&, J& strTmp = "COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9,COL10,COL11,COL12,COL13,COL14,COL15,COL16,COL17,COL18,COL19,COL20" strTmp = "," & strTmp & ","
I = InStr(1, strTmp, ",") + 1 J = InStr(I, strTmp, ",") - I While I > 0 And J > 0 strNew = strNew & "," & Right(Replace(Mid(strTmp, I, J), "COL", "00"), 3) I = InStr(I + J, strTmp, ",") + 1 J = InStr(I + 1, strTmp, ",") - I Wend strNew = Mid(strNew, 2) Debug.Print strNew End Sub
谢谢zjcxc(邹建) 但我的问题是如果那个字符串只包含了几个我想要替换的字符 例子: ”select * from aaa where COL1=COL2+COL3+COL22" "select * form aaa where COL23=COL2+COL22" 如果要把这两个语句替换成为 ”select * from aaa where 001=002+003+022" "select * form aaa where 023=002+022" 注意 替换后的字符要是三位的!COL1->001,COL11->011
dim strNew as string
strTmp="COL1,COL2,COL3....COL20"
StrNew=replace(strTmp,"Col","00")如果涉及到字符不固定,用循环逐一找出替换
我想要的结果是”001,002..020“
比如,是一条sql语句呢?
ex:"select * from aaa where col1=col11"
要转换成"select * from aaa where 001=011"
要怎么转换??
Dim strTmp As String
Dim strNew As String
Dim I&, J&
strTmp = "COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9,COL10,COL11,COL12,COL13,COL14,COL15,COL16,COL17,COL18,COL19,COL20"
strTmp = "," & strTmp & ","
I = InStr(1, strTmp, ",") + 1
J = InStr(I, strTmp, ",") - I
While I > 0 And J > 0
strNew = strNew & "," & Right(Replace(Mid(strTmp, I, J), "COL", "00"), 3)
I = InStr(I + J, strTmp, ",") + 1
J = InStr(I + 1, strTmp, ",") - I
Wend
strNew = Mid(strNew, 2)
Debug.Print strNew
End Sub
但我的问题是如果那个字符串只包含了几个我想要替换的字符
例子:
”select * from aaa where COL1=COL2+COL3+COL22"
"select * form aaa where COL23=COL2+COL22"
如果要把这两个语句替换成为
”select * from aaa where 001=002+003+022"
"select * form aaa where 023=002+022"
注意 替换后的字符要是三位的!COL1->001,COL11->011