| | |
| | | namespace Common |
| | | { |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Runtime.InteropServices; |
| | | using System.Text.RegularExpressions; |
| | | using System.Linq; |
| | | |
| | | public class DataConverter |
| | | { |
| | |
| | | [FieldOffset(0)] |
| | | public DataConverter.IPAdress _IP; |
| | | } |
| | | |
| | | //获取随机码 |
| | | public static string getRandom(int strLength) |
| | | { |
| | | string random = ""; |
| | | if (strLength <= 0) |
| | | { |
| | | strLength = 6; //默认6位长度 |
| | | } |
| | | //random = Guid.NewGuid().ToString().Replace("-", "").Substring(0, strLength); |
| | | random = Guid.NewGuid().ToString(); |
| | | random = random.Replace("-", ""); |
| | | random = random.Substring(0, strLength); |
| | | |
| | | return random; |
| | | } |
| | | |
| | | //字符串转数组 |
| | | public static object[] stringToArray(string str) |
| | | { |
| | | object[] array = str.Split(','); |
| | | return array; |
| | | } |
| | | |
| | | //数组转字符串 |
| | | public static string arrayToString(object[] array) |
| | | { |
| | | if (array == null) |
| | | return ""; |
| | | string str = string.Join(",", array); |
| | | return str; |
| | | } |
| | | |
| | | //数组转换List:string[] str ={ "str","string","abc"}转 List<string> |
| | | public static List<object> arrayToList(object[] array) |
| | | { |
| | | List<object> list = new List<object>(array); |
| | | |
| | | return list; |
| | | } |
| | | |
| | | //List转换数组:List<string>转到string[] |
| | | public static object[] listToArray(List<object> list) |
| | | { |
| | | object[] array = list.ToArray(); |
| | | |
| | | return array; |
| | | } |
| | | |
| | | //字符串转List |
| | | public static List<object> stringToList(string str) |
| | | { |
| | | if (string.IsNullOrEmpty(str)) |
| | | return null; |
| | | |
| | | List<object> list = new List<object>(); |
| | | //字符串转数组,再数组合并 |
| | | list.AddRange(str.Split(',')); |
| | | |
| | | return list; |
| | | } |
| | | |
| | | //数组去重,移除数组中重复数据 |
| | | public static string[] DelRepeatData(string[] array) |
| | | { |
| | | return array.GroupBy(p => p).Select(p => p.Key).ToArray(); |
| | | } |
| | | |
| | | //检查数组格式字符串中是否包含某元素 |
| | | public static bool checkStrForArrayStr(object str, string arrayStr ) |
| | | { |
| | | return stringToArray(arrayStr).Contains(str); |
| | | } |
| | | |
| | | //检查数组中是否包含某元素 |
| | | public static bool checkStrForArray(object str, object[] array) |
| | | { |
| | | return array.Contains(str); |
| | | } |
| | | |
| | | //根据数组字符串转换字典 |
| | | public Dictionary<string, object> arrayStrToDict(object userId, string arrayStr) |
| | | { |
| | | Dictionary<string, object> dictionary = new Dictionary<string, object>(); |
| | | if (string.IsNullOrEmpty(arrayStr)) |
| | | return dictionary; |
| | | |
| | | List<object> list = DataConverter.stringToList(arrayStr); |
| | | for (int i = 0; i < list.Count; i++) |
| | | { |
| | | dictionary[userId.ToString()] = list[i]; |
| | | } |
| | | return dictionary; |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |