提问人:Laila Firdaus Zakiyah 提问时间:10/24/2023 最后编辑:Laila Firdaus Zakiyah 更新时间:10/25/2023 访问量:62
如何从字符串中迭代每个字符并在特定字符 VB.net 中添加 vbcrlf [已关闭]
How to iterate each character from a string and add a vbcrlf in certain character VB.net [closed]
问:
所以,我有一个文本文件。目的是使用 VB.net 在字符串的某些部分添加 VBLF(换行符)。下面是字符串的示例:
之前(字符串没有任何换行符):
{1:F01ABCDIDJAXXXX0001212544}{2:O9001038231020QWERIDJRAXXX00011102422310201038N}{3:{113:0070}}{4::20:94493831555385/900:21:0799102034308001:25:520008000990:32A:231020USD0,:72:/ESETDATE/2310201038+0000/OID/231020ABCDDJAXXXX0001019040/AMNT/10000000000001,12-}
后:
如果我们在记事本上看到 ++ 将看起来像这里打开的图像
我认为我们需要遍历每个字符。 以下是我的逻辑:
1.寻找角色
{4:
,然后替换为 {4: + vbLf
- 替换正则表达式
对于每个带有图案的字符
^:[\d\w]+:
(喜欢 :20: ) 然后替换为 vbLf + originalcharacter
- 替换正则表达式
对于每个带有图案的字符
/(.*?)/
(如 \AMNT\ )
然后替换为 vbLf + originalcharacter
如果有人可以帮助解决这个问题,请不胜感激。谢谢
答:
0赞
Nick Abbot
10/25/2023
#1
以下是不使用正则表达式的一种可能解决方案:
Sub test()
Dim YourString As String = "{1:F01ABCDIDJAXXXX0001212544}{2:O9001038231020QWERIDJRAXXX00011102422310201038N}{3:{113:0070}}{4::20:94493831555385/900:21:0799102034308001:25:520008000990:32A:231020USD0,:72:/ESETDATE/2310201038+0000/OID/231020ABCDDJAXXXX0001019040/AMNT/10000000000001,12-}"
Debug.Print(DoIt(YourString))
End Sub
Function DoIt(sLine As String) As String
Dim aTemp1() As String
Dim aTemp2() As String
Dim aTemp3() As String
Dim sResult As String
Dim i As Integer
Try
aTemp1 = sLine.Split("{4:")
aTemp2 = aTemp1(1).Split(":", StringSplitOptions.RemoveEmptyEntries)
aTemp3 = aTemp2.Last.Split("/", StringSplitOptions.RemoveEmptyEntries)
'"{1:F01ABCDIDJAXXXX0001212544}{2:O9001038231020QWERIDJRAXXX00011102422310201038N}{3:{113:0070}}{4:" & vbLf
sResult = aTemp1(0) & "{4:" & vbLf
'":20:94493831555385/900" & vbLf & ":21:0799102034308001" & vbLf & ":25:520008000990" & vbLf & ":32A:231020USD0," & vbLf
For i = 0 To aTemp2.Length - 3 Step 2
sResult &= ":" & aTemp2(i) & ":"
sResult &= aTemp2(i + 1) & vbLf
Next
'":72:"
sResult &= ":" & aTemp2(i) & ":"
'/ESETDATE/2310201038+0000" & vbLf & "/OID/231020ABCDDJAXXXX0001019040" & vbLf & "/AMNT/10000000000001,12-}" & vbLf
For i = 0 To aTemp3.Length - 1 Step 2
sResult &= "/" & aTemp3(i) & "/" & aTemp3(i + 1) & vbLf
Next
'vbLf & "-}"
sResult = sResult.Replace("-", vbLf & "-") ' <-- You forgot this part
Return sResult
Catch ex As Exception
Return String.Empty
End Try
End Function
评论
0赞
Laila Firdaus Zakiyah
10/26/2023
非常感谢方丈@nick帮助,这可以作为我的额外参考。我尝试过使用正则表达式,幸运的是它有效。
评论