开发者

How to create CSV string from String Array, If string is blank at any index than just discard it

Let's say I have array like {"12","23","","34","22","","","12"}

If there are 5 items as than it should append "<br/> after 2 items. I want to arrange this in string like "12,23,</br>34,22</br>,12";

If there are 3 items in array string should break after 2 items.

There will be max 7 items in array.开发者_如何学JAVA


Is this (pseudo-code) something like what you want? I believe your goal is to collapse multiple empty elements into a single <br/> element; if that is not accurate, please try to clarify your intention.

sb = new stringBuilder
justWroteBr = false
foreach s in array
    if s == ""
        if justWroteBr
            continue
        else
            s.Add("<br/>")
            justWroteBr = true
        end
    else
        s.Add(s + ",")
        justWroteBr = false
    end
next


var yourArray = ...;
yourArray = yourArray.Where(str => !string.IsNullOrWhiteSPace(str)).ToArray();

var yourResult = Enumerate
  .Range(0, yourArray.Length)
  .Select(index => (index % 3 == 0)?("<br>" + yourArray[index]):yourArray[index])
  .Aggregate((cur, nex) => cur + "," + nex);

change Aggregate to ToList().ForEach(...stringbuilder.Append ) for better performance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜