问题描述
(限制:系统;仅)
我希望能够将字符串拆分为数组并删除空格,目前我有这个:
I want to be able to split a string into an array and remove the spaces, I have this currently:
string[] split = converText.Split(',').Select(p => p.Trim()).ToArray();
.ToArray显然也不能使用.
Also .ToArray cant be used apparently.
但是问题是,除了核心系统方法之外,我不能再使用其他任何方法.因此,如何在不使用.select或其他非核心方式的情况下,从拆分或数组中修剪空间.
But the problem is, I can't use anything other then core system methods. So how can i trim spaces from a split or array without using .select or other non core ways.
谢谢!
推荐答案
string[] split =
convertText.Split(new[]{',',' '}, StringSplitOptions.RemoveEmptyEntries);
通过在拆分条件中添加一个空格,当您具有RemoveEmptyEntries时,它将消除它们.但是,如果其中包含空格,则此操作将失败.在这种情况下,您可以:-
by adding a space to your split criteria, it will get rid of them when you have RemoveEmptyEntries. However this will fail if there are entries with spaces in them. In which case you could just :-
string[] split =
convertText.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries);
for (int index = 0; index < split.Count; index++)
{
split[index] = split[index].Trim();
}
这篇关于分割字符串并删除不带.select的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!