我有以下变数.
Dim strValues As String(),其值为"1","2","3","4","5","6","7"
我想根据字符串索引从StrValues中删除"4"而不知道vb.net中的字符串值.我怎样才能做到这一点?
我简单地使用Linq:
Dim strValues = {"1", "2", "3", "4", "5", "6", "7"}
strValues = strValues.Where(Function(s) s <> "4").ToArray 'Removes all instances
Run Code Online (Sandbox Code Playgroud)
更好的是,使用列表:
Dim strValues = {"1", "2", "3", "4", "5", "6", "7"}.ToList
strValues.Remove("4") 'Removes first instance of "4" only
Run Code Online (Sandbox Code Playgroud)
如果你想通过索引值来做,你可以做类似下面的事情(尽管这些将删除值的所有实例):
Dim index = 3
strValues = strValues.Where(Function(s) s <> strValues(index)).ToArray
Run Code Online (Sandbox Code Playgroud)
要么
strValues = strValues.Except({strValues(index)}).ToArray
Run Code Online (Sandbox Code Playgroud)
要删除或跳过给定索引(仅限单个实例),您可以执行以下操作:
Dim index = 3
strValues = strValues.Take(index).Concat(strValues.Skip(index + 1)).ToArray
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18150 次 |
| 最近记录: |