我有这个字符串
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
并想要检索字符串
newstr = 12,32,15,16,14
我试了这么多
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
Dim word As String
Dim uc As String() = test.Split(New Char() {","c})
For Each word In uc
' What can i do here?????????
Next
只有唯一的数字我怎么能在vb asp.net中做到这一点
正确答案
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
Dim word As String
Dim uc As String() = test.Split(New Char() {","c}).Distinct.ToArray
Dim sb2 As String = "-1"
For Each word In uc
sb2 = sb2 + "," + word
Next
MsgBox(sb2.ToString)
答案 0 :(得分:6)
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
Dim uniqueList As String() = test.Split(New Char() {","c}).Distinct().ToArray()
答案 1 :(得分:2)
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
'Split into an array
Dim testArray As String() = test.Split(",")
'remove duplicates
Dim uniqueTestArray As String() = testArray.Distinct().ToArray())
'Concatenate back to string
Dim uniqueString As String = String.Join(",", uniqueTestArray)
或全部在一行:
Dim uniqueString As String = String.Join(",", test.Split(",").Distinct().ToArray())
答案 2 :(得分:0)
更新对不起我忘记将新字符串添加到一起
<强>解决方案:强>
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
Dim distinctArray = test.Split(",").Distinct()
Dim newStr As String = String.Join(",", distinctArray.Distinct().ToArray())
培训参考资料:查看本网站以获取LINQ指南,这将使您更轻松地应对这些类型的编程挑战。 LINQ Tutorial
答案 3 :(得分:0)
您忘记为 Distinct 和 ToArray 加上括号。因为这些都是方法
Dim uc As String() = test.Split(New Char() {","c}).Distinct().ToArray()