vb.net 从数字字符串中检索唯一值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13428151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
retrieve unique values from string of numbers
提问by user1820110
i have this string
我有这个字符串
Dim test As String = "12,32,12,32,12,12,32,15,16,15,14,12,32"
and want to retrieve a string
并想检索一个字符串
newstr = 12,32,15,16,14
i tried this much
我试了这么多
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
only unique numbers how can i do that in vb asp.net
只有唯一的数字我怎么能在 vb asp.net 中做到这一点
right answer
正确答案
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)
回答by Naresh Pansuriya
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()
回答by Steve
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)
Or all in one line:
或者全部在一行中:
Dim uniqueString As String = String.Join(",", test.Split(",").Distinct().ToArray())
回答by MVCKarl
Updated Sorry I forgot to add the new string together
更新抱歉我忘了将新字符串添加在一起
Solution:
解决方案:
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())
Training References:Check out this website for a guide on LINQ which will make these types of programming challenges easier for you. LINQ Tutorial
培训参考:查看此网站以获取有关 LINQ 的指南,该指南将使您更轻松地应对这些类型的编程挑战。LINQ 教程

