string[] B = C.OfType<object>().Where(o =>o != null).Select(o => o.ToString()).ToArray();
我尝试将数组C
转换为string[]
但是数组C
有很多null
,我希望将null
更改为" "
我该怎么办?
You don't need OfType<object>()
, simply call string.Concat :
var s = string.Concat(C.ToArray());
If you look at documentation:
The method concatenates each object in args by calling the parameterless ToString method of that object; it does not add any delimiters. String.Empty is used in place of any null object in the array.'
This should work:
var B = C.OfType<string>().Select(o => o ?? " ").ToArray();
.OfType<string>()
will filter out all non-string values from C.
.Select(o => o ?? " ")
will select the value, or " "
if the value was null.
and .ToArray()
will take the IEnumerable and turn it into a string[]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.