我正在嘗試接受某人輸入的批處理名稱并通過回圈運行它以用“_”替換和非法字符(!£$%...等)。這是我所擁有的:
'define illegal chars
illegalChars = "!,£,$,%,^,&,*"
'split illegal chars into an array
illegalCharsArr = Split(illegalChars, ",")
'take entered data from textbox
batch_name = batchEntryBox.Text
'split entered data into an array
batchNameArr = Split(batch_name, " ")
For Each item In batchNameArr
For Each i In illegalCharsArr
If batchNameArr(item) = illegalCharsArr(i) Then
batchNameEdit = batchNameArr.Replace(batchNameArr(item), illegalCharsArr(i))
End If
Next
Next
'post new batch name in Label box
Label1.Caption = batchNameEdit
End Sub
我無法讓它作業。另外,如何拆分沒有空格的名稱?
謝謝!
uj5u.com熱心網友回復:
試試這個邏輯。
Sub CleanTheWord()
illegalChars = "!,£,$,%,^,&,*"
Dim illegalCharsArr() As String
illegalCharsArr = Split(illegalChars, ",")
Dim batch_name As String
batch_name = "£sadg$u^su&bc!ksj*" 'Changed this for testing. Modify accordingly
Dim newstring As String
newstring = batch_name
Dim pos As String
For j = 1 to Len(batch_name)
pos = Mid(batch_name, j, 1)
For Each i In illegalCharsArr
If StrComp(pos, i, vbTextCompare) = 0 Then
newstring = VBA.Replace(newstring, pos, "_", 1, 1, vbTextCompare)
End If
Next
Next
MsgBox newstring, vbInformation
End Sub
嘗試和測驗:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/384927.html
標籤:网络
上一篇:使用Json.NET從VB.NET中的JSON獲取資訊
下一篇:向使用GDI 繪制的影像添加文本
