using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Linq.Expressions;
namespace WalkingTec.Mvvm.Core.Extensions
{
///
/// 字符串辅助类
///
public static class StringExtension
{
///
/// 根据名字获取Id形式
///
/// 名字
/// 将[].转换成_形式的Id
public static string GetIdByName(this string fieldName)
{
return fieldName == null ? "" : fieldName.Replace(".", "_").Replace("[", "_").Replace("]", "_");
}
///
/// 格式化URL
///
/// 初始url
/// 格式化后的url
public static string CorrectUrl(this string url)
{
if (string.IsNullOrWhiteSpace(url) == true)
{
url = "";
}
else
{
url = url.ToLower();
url = url.Trim('/', '\\');
if (url.StartsWith("http://") == false && url.StartsWith("https://") == false)
{
url = "http://" + url;
}
}
return url;
}
///
/// 将数据列表转化为逗号分隔的字符串
///
/// 源数据类
/// 文本字段
/// 源数据List
/// 要拼接的文本字段
/// 转化文本字段的表达式
/// 分隔符,默认为逗号
/// 转化后的字符串
public static string ToSepratedString(this IEnumerable self, Expression> textField, Func Format = null, string seperator = ",")
{
string rv = "";
if (self == null)
{
return rv;
}
//循环所有数据
for (int i = 0; i < self.Count(); i++)
{
//获取文本字段的值
V text = textField.Compile().Invoke(self.ElementAt(i));
string str = "";
//如果有转换函数,则调用获取转换后的字符串
if (Format == null)
{
if (text == null)
{
str = "";
}
else
{
str = text.ToString();
}
}
else
{
str = Format.Invoke(text);
}
rv += str;
//拼接分隔符
if (i < self.Count() - 1)
{
rv += seperator;
}
}
//返回转化后的字符串
return rv;
}
public static string ToSepratedString(this IEnumerable self, Func