如何替换lastindexof“&引用;对“一词”;及;但要避免“替换”;及;在vb.net中的开括号和闭括号内
如何在vb.net的开括号和闭括号内替换最后一个精度“、”到“和”,但避免替换“和” 样本: A、 A1、A2、A3、B、B1、B2、B3、C、C1、C2、C3 我想要这样的输出 A(A1、A2、A3)、B(B1、B2、B3)和C(C1、C2、C3) 这是我的代码: Dim Strng As String=Me.CheckBox1.Text如何替换lastindexof“&引用;对“一词”;及;但要避免“替换”;及;在vb.net中的开括号和闭括号内,vb.net,Vb.net,如何在vb.net的开括号和闭括号内替换最后一个精度“、”到“和”,但避免替换“和” 样本: A、 A1、A2、A3、B、B1、B2、B3、C、C1、C2、C3 我想要这样的输出 A(A1、A2、A3)、B(B1、B2、B3)和C(C1、C2、C3) 这是我的代码: Dim Strng As String=Me.CheckBox1.Text 'now find the position of last appearing "," Dim comaposition As Integ
'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
我怎样才能做到这一点?有什么想法吗?这里有一个解决方案:
Dim test As String = "A,A1,A2,A3,B,B1,B2,B3,C,C1,C2,C3"
'this gets A,B,C
Dim SingleChar As String() = test.Split(",").Where(Function(a) Not a.Any(Function(c) Char.IsDigit(c))).ToArray()
'this gets rest of the string
Dim CharWithNumbers As String() = test.Split(",").Where(Function(a) a.Any(Function(c) Char.IsDigit(c))).ToArray()
Dim StrFinal As String = ""
Dim diction = New Dictionary(Of String, List(Of String))()
Dim list = New List(Of String)()
'This will add similar strings found in "CharWithNumbers" based on
'the characters in "SingleChar" to a list
For Each c As char In SingleChar
list = New List(Of String)()
diction.Add(c, list)
For Each item As string In CharWithNumbers
If item.Contains(c) Then
list.Add(item)
End If
Next
Next
'grouping the list
For Each item In diction
StrFinal += item.Key + "(" + String.Join(",", item.Value) + "),"
Next
StrFinal = StrFinal.TrimEnd(","C) 'removes trailing ','
'to replace "," with "and"
Dim LastChar = SingleChar(SingleChar.Count() - 1)
StrFinal = StrFinal.Replace("," + LastChar + "(", " and " + LastChar + "(")
Console.WriteLine(StrFinal)
输出:
A(A1,A2,A3),B(B1,B2,B3) and C(C1,C2,C3)
这不是针对性能而优化的,但我认为您不会为此烦恼
检查中的输出这里有一个解决方案:
Dim test As String = "A,A1,A2,A3,B,B1,B2,B3,C,C1,C2,C3"
'this gets A,B,C
Dim SingleChar As String() = test.Split(",").Where(Function(a) Not a.Any(Function(c) Char.IsDigit(c))).ToArray()
'this gets rest of the string
Dim CharWithNumbers As String() = test.Split(",").Where(Function(a) a.Any(Function(c) Char.IsDigit(c))).ToArray()
Dim StrFinal As String = ""
Dim diction = New Dictionary(Of String, List(Of String))()
Dim list = New List(Of String)()
'This will add similar strings found in "CharWithNumbers" based on
'the characters in "SingleChar" to a list
For Each c As char In SingleChar
list = New List(Of String)()
diction.Add(c, list)
For Each item As string In CharWithNumbers
If item.Contains(c) Then
list.Add(item)
End If
Next
Next
'grouping the list
For Each item In diction
StrFinal += item.Key + "(" + String.Join(",", item.Value) + "),"
Next
StrFinal = StrFinal.TrimEnd(","C) 'removes trailing ','
'to replace "," with "and"
Dim LastChar = SingleChar(SingleChar.Count() - 1)
StrFinal = StrFinal.Replace("," + LastChar + "(", " and " + LastChar + "(")
Console.WriteLine(StrFinal)
输出:
A(A1,A2,A3),B(B1,B2,B3) and C(C1,C2,C3)
这不是针对性能而优化的,但我认为您不会为此烦恼
检查中的输出尝试以下操作:
Dim arr() As String = Regex.Matches("A,A1,A2,A3,B,B1,B2,B3,C,C1,C2,C3", "(.)(,\1.+?)+").Cast(Of Match).Select(Function(M As Match) Regex.Replace(M.Value, "^(.),", "$1(") & ")").ToArray()
Dim withoutLastMatch(arr.Length - 2) As String
Array.Copy(arr, withoutLastMatch, arr.Length - 1)
Dim output As String = String.Join(",", withoutLastMatch) & " and " & arr(arr.Length - 1)
Console.WriteLine(output)
输出为:
A(A1、A2、A3)、B(B1、B2、B3)和C(C1、C2、C3)
检查位于的解决方案尝试以下操作:
Dim arr() As String = Regex.Matches("A,A1,A2,A3,B,B1,B2,B3,C,C1,C2,C3", "(.)(,\1.+?)+").Cast(Of Match).Select(Function(M As Match) Regex.Replace(M.Value, "^(.),", "$1(") & ")").ToArray()
Dim withoutLastMatch(arr.Length - 2) As String
Array.Copy(arr, withoutLastMatch, arr.Length - 1)
Dim output As String = String.Join(",", withoutLastMatch) & " and " & arr(arr.Length - 1)
Console.WriteLine(output)
输出为:
A(A1、A2、A3)、B(B1、B2、B3)和C(C1、C2、C3)
检查位于的解决方案不言而喻,您自己尝试过什么吗?如果是,发布这些代码。如果你能够成功地将字符分组为
(
和)
,我的意思是A(A1,A2,A3),B(B1,B2,B3),C(C1,C2,C3)
@没有人,我只是使用了他的代码,它除了在前面的末尾加上“和”之外什么都没有c3@Subaz我已经解决了这个问题,只是想知道OP的输入。我知道贴出的代码根本不能作为输出:不用说,你自己试过什么吗?如果是,发布这些代码。如果你能够成功地将字符分组为(
和)
,我的意思是A(A1,A2,A3),B(B1,B2,B3),C(C1,C2,C3)
@没有人,我只是使用了他的代码,它除了在前面的末尾加上“和”之外什么都没有c3@Subaz我已经解决了这个问题,只是想知道OP的输入。我知道发布的代码作为输出根本不起作用:Dcan你能解释一下这个Dim SingleChar as String()=test.Split(“,”).Where(函数(a)Not a.Any(函数(c)Char.IsDigit(c)).ToArray()
这将搜索没有数字的字符串。如果你仔细看,你会看到一个不可能的结果Dim CharWithNumbers…
是相同的东西,但没有,因此它将占用字符串的其余部分,例如字符串是“A,A1,A2,A3,B,B,B1,B2.B3”。它将返回A,B,B?是的,我的解决方案适用于提供的字符串format@Subaz这将导致他的diction
dictionary变量出错,因为B已经被添加为键。该场景需要某种形式的错误处理。请将此Dim SingleChar解释为String()=test.Split(“,”)。其中(函数(a)不是a.Any(函数(c)Char.IsDigit(c)))。ToArray()
这将搜索其中没有数字的字符串。如果你仔细看,你会看到一个不可能的结果Dim CharWithNumbers…
是相同的东西,但没有,因此它将占用字符串的其余部分,例如字符串是“A,A1,A2,A3,B,B,B1,B2.B3”。它将返回A,B,B?是的,我的解决方案适用于提供的字符串format@Subaz这将导致他的diction
dictionary变量出错,因为B已经被添加为键。这种情况下需要某种形式的错误处理。我更喜欢你的解决方案。简明扼要。让我投票:)这个怎么样我的字符串=英语语法(动词、名词和形容词)、数学(代数、三角测量和几何)、科学(物理、化学和生态学)我想要这样的输出:英语语法(动词、名词和形容词)、数学(代数、三角学和几何)和科学(物理、化学和生态学)我不知道该怎么做。你能帮我解决这个问题吗?我使用这个代码Dim arr()作为String=Regex.Matches(ProvinceCagayan.Text,“(()(,\1.+?)+”)”).Cast(Of Match)。选择(函数(M As Match)Regex.Replace(M.Value,“^(),”,“$1(“)&”)。ToArray()用outlastmatch(arr.Length-2)调暗As String Array.Copy(arr,withoutletmatch,arr.Length-1)将输出变暗为String=String.Join(“,”,withoutletmatch)和“&arr(arr.Length-1)ProvinceCagayan.Text=output.ToString,但我得到了错误”System.OverFlowException“如何解决它我喜欢你的解决方案比我的好。简短而简单。让我投票:)这个怎么样我的字符串=英语语法(动词、名词和形容词)、数学(代数、三角测量和几何)、科学(物理、化学和生态)我想要这样的输出:英语语法(动词、名词和形容词)、数学(代数、三角学和几何)和科学(物理、化学和生态学)我不知道该怎么做。你能帮我解决这个问题吗?我使用这个代码Dim arr()作为String=Regex.Matches(ProvinceCagayan.Text,“(()(,\1.+?)+”)”).Cast(Of Match)。选择(函数(M As Match)Regex.Replace(M.Value,“^(),”,“$1(“)&”)。ToArray()用outlastmatch(arr.Length-2)调暗As String Array.Copy(arr,WithOutletMatch,arr.Length-1)将输出变暗为String=String.Join(“,”,WithOutletMatch)和“&arr(arr.Length-1)ProvinceCagayan.Text=output.ToString,但我遇到了错误”System.OverFlowException“如何修复它?”