对包含#
和without #
Dim Strfinal as String = "#Cccccc,#Aaaaaa,Aaaaa,Baaaa,Caaaa,#Bbbbbb,Abbbbb,Bbbbbb,Cbbbbb"
我想要像这样的输出
#Cccccc, #Aaaaaa(Aaaaa,Baaaa and Caaaa) and #Bbbbbb(Abbbbb,Bbbbbb and Cbbbbb)
我使用此代码分隔没有#
Dim rws As String
If Aaaaa.Checked = True Then
CheckBox1.Checked = True
close_parenthesis.Checked = True
If rws = "" Then
rws = "(" + Aaaaa.Text
Else
rws = "(" + Aaaaa.Text
End If
End If
If Aaaaa.Checked = False Then
rws = ""
End If
If Baaaa.Checked = True Then
CheckBox1.Checked = True
close_parenthesis.Checked = True
If rws = "" Then
rws = "(" + Baaaa.Text
Else
rws = rws & ", " & Baaaa.Text
End If
End If
If Caaaa.Checked = True Then
CheckBox1.Checked = True
close_parenthesis.Checked = True
If rws = "" Then
rws = "(" + Caaaa.Text
Else
rws = rws & ", " & Caaaa.Text
End If
End If
If close_parenthesis.Checked = True Then
If rws = "" Then
Else
rws = rws + close_parenthesis.Text
End If
End If
CheckBox1.Text = rws.ToString
Me.CagayanReplace.PerformClick()
我使用此代码将,
更改为括号内的and
字
Dim Strng As String = Me.CheckBox1.Text
'now find the position of last appearing ","
Dim comaposition As Integer
comaposition = Strng.LastIndexOf(",") 'it is zero based
'if not found, it will return -1 and u can exit, no need to do the work
If comaposition = "-1" Then
Exit Sub
End If
'remove the comma
Dim String_After_Removing_Comma As String
String_After_Removing_Comma = Strng.Remove(comaposition, 1)
'add "and" in the same position where comma was found
Dim final_string As String
final_string = String_After_Removing_Comma.Insert(comaposition, " and ")
'show it on the textbox
CheckBox1.Text = final_string`
请帮我解决。
答案 0 :(得分:0)
这不是一个漂亮的解决方案,可以进行很多优化。但它完成了工作:
Dim Strfinal As String = "#Cccccc,#Aaaaaa,Aaaaa,Baaaa,Caaaa,#Bbbbbb,Abbbbb,Bbbbbb,Cbbbbb"
Dim splittedString As String() = Strfinal.Split(",")
Dim a As List(Of String) = New List(Of String)
Dim newstr As String = ""
For Each str As String In splittedString
If Not str.Contains("#"c) Then
a.Add(str)
Continue For
End If
If a.Count > 0 Then
Dim b = String.Join(",", a)
Dim i = b.LastIndexOf(","c)
b = b.Remove(i, 1)
b = b.Insert(i, " and ")
newstr += "(" + b + ")," + str
a.Clear()
Else
newstr += str + ","
End If
Next
If a.Count > 0 Then
Dim b = String.Join(",", a)
Dim i = b.LastIndexOf(","c)
b = b.Remove(i, 1)
b = b.Insert(i, " and ")
newstr += "(" + b + ")"
a.Clear()
End If
newstr = newstr.Replace(",(", " (")
Dim z = newstr.LastIndexOf(",#")
newstr = newstr.Remove(z, 1)
newstr = newstr.Insert(z, " and ")
Console.WriteLine(newstr)
输出:
#Cccccc,#Aaaaaa (Aaaaa,Baaaa and Caaaa) and #Bbbbbb(Abbbbb,Bbbbbb and Cbbbbb)
上查看