分隔并分组具有#和不具有#的字符串

vb.net

62 观看

1回复

17 作者的声誉

分隔并分组具有#和的字符串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`

请帮我解决。

作者: Paddayuman Teddy 的来源 发布者: 2017 年 9 月 15 日

回应 1


0

2265 作者的声誉

这不是一个很好的解决方案,可以进行很多优化。但这可以完成工作:

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)

小提琴上检查

作者: boop_the_snoot 发布者: 2017 年 9 月 15 日
32x32