VB如何替换不同的字符
请教如何使用replace函数或者其他的方法替换字符串比如要把"+967-2333-2333-2333"替换为"X96.70Y23.33I23.33J23.33
类似于这样的,本来可以通过截取"+"和"-"的位置来替换,但是发现它的位置不定,就是说数字的长度不定
用replace写成这样sStr = Replace(sStr, "+", "X", 1, 1)只能替换一次,后面的无法替换
像这种情况应该怎么处理啊
--------------------编程问答--------------------
dim s as string,a() as string--------------------编程问答-------------------- 不好意思,我把字符串写错了,+967-2333-2333-2333应该替换成X9.67Y-23.33I-23.33J-23.33
s = "+967-2333-2333-2333"
a = split(replace(s,"+","-"),"-")
s = "X" & a(0) & "Y" & a(1) & "I" & a(2) & "J" & a(3)
修改了一下你的代码,已经实现了一部分,但是后面的"-"没有
dim s as string,a() as string
s = "+967-2333-2333-2333"
a = split(replace(s,"+","-"),"-")
s = "X" & a(1)/100 & "Y" & a(2)/100 & "I" & a(3)/100 & "J" & a(4)/100
另外还是想问一下,在文件里有很多行类似的数据,比如
-707+707
+0+78653
+19062+27799+29800+0-
+28971+11190
+3531+658+3531-9142-
+136+0
+141+59
+59+141
像这样,是否可以根据内容判断,-707+707替换成X-707Y707,
967-2333-2333-2333替换成X9.67Y-23.33I-23.33J-23.33 --------------------编程问答-------------------- 检查 ubound(a),返回 1 就是 XY 的格式,返回 3 就是 XYIJ 的格式。 --------------------编程问答-------------------- sStr = Replace(sStr, "+", "X")
sStr = Replace(sStr, "-", ".") --------------------编程问答-------------------- a = split(replace(s, "+ ", "- "), "- ")
截取到字符后,符号丢失了
X9.67Y23.33I23.33J23.33
有没有办法能保留"-"
+967-2333-2333-2333替换成X9.67Y-23.33I-23.33J-23.33
--------------------编程问答-------------------- test.txt:
-707+707
+0+78653
+19062+27799+29800+0-
+28971+11190
+3531+658+3531-9142-
+136+0
+141+59
+59+141
Dim strLine As String, strNewString As String
Dim strTmp() As String
Open "c:\test.txt" For Input As #1
Do Until EOF(1)
strNewString = ""
Line Input #1, strLine
strLine = Replace(strLine, "+", Space(1))
strLine = Replace(strLine, "-", Space(1) & "-")
strTmp = Split(Trim(strLine), Space(1))
strNewString = "X" & Format(Val(strTmp(0)) / 100, "standard")
If UBound(strTmp) >= 1 Then strNewString = strNewString & "Y" & Format(Val(strTmp(1)) / 100, "standard")
If UBound(strTmp) >= 2 Then strNewString = strNewString & "I" & Format(Val(strTmp(2)) / 100, "standard")
If UBound(strTmp) >= 3 Then strNewString = strNewString & "J" & Format(Val(strTmp(3)) / 100, "standard")
Debug.Print strNewString
Loop
Close #1
结果:
X-7.07Y7.07
X0.00Y786.53
X190.62Y277.99I298.00J0.00
X289.71Y111.90
X35.31Y6.58I35.31J-91.42
X1.36Y0.00
X1.41Y0.59
X0.59Y1.41 --------------------编程问答-------------------- 奇怪,我输出的结果好像乱的很
X 48ta4023ar23
X 0ta030ar30
X 48ta528ar8 Y 0ta030ar30
X 24ta215ar15
X 24ta263ar3 Y 0ta030ar30
X 24ta3831ar31
X 24ta5031ar31
X 36ta930ar30 --------------------编程问答--------------------
--------------------编程问答-------------------- 我是个新手啊,Function应该怎么调用的啊 --------------------编程问答-------------------- 我是个新手啊,Function应该怎么调用的啊
Function ReplaceString(ByVal s As String) As String
Dim sList As String
Dim arr() As String
Dim i As Integer
s = Replace(s, "+", "/")
s = Replace(s, "-", "/-")
arr = Split(s, "/")
For i = 1 To UBound(arr)
sList = Choose(i, "X", "Y", "I", "J", "")
If arr(i) <> "-" And arr(i) <> "" Then
ReplaceString = ReplaceString & _
sList & Format(arr(i) / 100, "0.00")
End If
Next
End Function
-------------------------------------------------------------
--------------------编程问答-------------------- 顶一下,学习! --------------------编程问答-------------------- ReplaceString(s)里面跟实参数
dim s as string
s=ReplaceString("+967-2333-2333-2333")
debug.print s
s=ReplaceString("+3531+658+3531-9142-")
debug.print s
如果他放在模块里面
call ReplaceString(s) --------------------编程问答-------------------- 学习
补充:VB , 基础类