我有一个字符串数组。我需要从该数组中删除一些项目。但我不知道需要删除的项目的索引。
我的数组是:string [] arr = {“”,“a”,“b”,“”,“c”,“”,“d”,“”,“e”,“f”,“ “,”“}。
我需要删除“”项目。即删除后“”我的结果应该是 ARR = { “一”, “B”, “C”, “d”, “E”, “F”}
我该怎么做?
答案 0 :(得分:10)
string[] arr = {" ", "a", "b", " ", "c", " ", "d", " ", "e", "f", " ", " "};
arr = arr.Where(s => s != " ").ToArray();
答案 1 :(得分:6)
这将删除所有空,空或只是空格的条目:
arr.Where( s => !string.IsNullOrWhiteSpace(s)).ToArray();
如果由于某种原因你只想删除只有一个空格的条目,就像你的例子一样,你可以像这样修改它:
arr.Where( s => s != " ").ToArray();
答案 2 :(得分:2)
使用LinQ
using System.Linq;
string[] arr= {" ","a","b"," ","c"," ","d"," ","e","f"," "," "}.
arr.Where( x => !string.IsNullOrWhiteSpace(x)).ToArray();
或者取决于你如何填充阵列,你可以在
之前完成string[] arr = stringToBeSplit.Split('/', StringSplitOptions.RemoveEmptyEntries);
然后空条目将不会首先放入您的字符串数组
答案 3 :(得分:0)
您可以使用Except删除空白值
sync_proc : process(clk_i)
begin
if(reset_i = '1') then
reset smt..
else(rising_edge(clk_i)) then
do smt..
end if;
end process sync_proc;