/// <summary> /// 字符拼接 /// </summary> /// <typeparam name="T">要拼接的对象类型</typeparam> /// <param name="enumerator">迭代器</param> /// <param name="separator">分隔符</param> /// <param name="logic">处理逻辑</param> /// <returns></returns> public static string Join <T>(this IEnumerator enumerator, string separator, StringJoinAction <T> logic) { if (enumerator == null) { return(String.Empty); } StringBuilder buf = new StringBuilder(); if (logic != null) { while (enumerator.MoveNext()) { if (enumerator.Current != null) { var s = logic((T)enumerator.Current); if (s != null) { buf.Append(s); buf.Append(separator); } } } } else { while (enumerator.MoveNext()) { if (enumerator.Current != null) { buf.Append(enumerator.Current.ToString()); buf.Append(separator); } } } if (buf.Length > 0) { var re = buf.ToString(); return(re.Substring(0, re.Length - separator.Length)); } return(String.Empty); }
/// <summary> /// 字符拼接 /// </summary> /// <typeparam name="T">要拼接的对象类型</typeparam> /// <param name="els">要拼接的对象</param> /// <param name="separator">分隔符</param> /// <param name="logic">处理逻辑</param> /// <returns></returns> public static string Join <T>(this IEnumerable <T> els, string separator, StringJoinAction <T> logic) { return(els.GetEnumerator().Join <T>(separator, logic)); }