[ClientTool] 项目里用到的工具func

本文主要介绍了在项目中使用的客户端工具ClientTool,包括其在数据处理方面的应用,如如何根据特定格式拆分数据,例如将1,2,3,;2,3,4等进行拆分。" 119570110,7477322,使用CSS clip-path创建不规则形状详解,"['前端', 'CSS']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义一个数据类吧 - 正常根据项目而定

public class ItemDataStruct
{
	public int _Id;
	public int _Num;

	public ItemDataStruct()
	{
	}

	public ItemDataStruct(int id, int num)
	{
		Init(id,num);
	}

	public ItemDataStruct(PBClientClass.PBPair pb)
	{
		if(pb == null)
		{
			Init(0,0);
			return;
		}
		Init(pb.key,pb.value);
	}

	public void Init(int id, int num)
	{
		_Id = id;
		_Num = num;
	}

	public static ItemDataStruct GetStructByStr(string str)
	{
		ItemDataStruct data = null;
		string[] items = str.Split(',');
		if(items.Length == 2)
		{
			data = new ItemDataStruct();
			int id;
			int num;
			if(int.TryParse(items[0],out id) && int.TryParse(items[1],out num))
			{
				data.Init(id,num);
			}
		}
		return data;
	}
}
1.10016,10;10017,12; 拆分成 上面结构数组

public static List<ItemDataStruct> GetItemDataStruct(string str)
	{
		// 计算md5缓存中是否存在
		string md5 = ClientTools.GetMD5(System.Text.Encoding.UTF8.GetBytes(str));
		if (_ItemDatas.ContainsKey(md5))
		{
			return _ItemDatas[md5];
		}

		List<ItemDataStruct> list = new List<ItemDataStruct>();
		if(str.Contains(",") == false)
		{
			return list;
		}
		string[] items = str.Split(';');
		for (int i = 0; i < items.Length; i = i + 1)
		{
			if(string.IsNullOrEmpty(items[i]))
			{
				continue;
			}
			string[] item = items[i].Split(',');
			if (item.Length == 2)
			{
				ItemDataStruct data = new ItemDataStruct(int.Parse(item[0]), int.Parse(item[1]));
				list.Add(data);
			}
			else
			{
				Debug.LogError(" data error");
			}
		}
		if(list.Count == 0)
		{
			return list;
		}
		_ItemDatas.Add(md5, list);
		return list;
	}
2.  1,1|2,2

 public static List<ItemNumData> GetItemNumDataListByStr(string str, char division = '|')
	{
		List<ItemNumData> list = new List<ItemNumData>();
		list.Clear();
		if (!string.IsNullOrEmpty(str) && !string.Equals(str, "nil"))
		{
			string[] words = str.Split(',');
			for (int i = 0; i < words.Length; i++)
			{
				ItemNumData attr = new ItemNumData();
                string[] temp = words[i].Split(division);
				if (temp.Length >= 2)
				{
					if (!string.IsNullOrEmpty(temp[0]))
					{
						attr.id = int.Parse(temp[0]);
					}
					if (!string.IsNullOrEmpty(temp[1]))
					{
						attr.num = int.Parse(temp[1]);
					}
					list.Add(attr);
				}
			}
		}
		
		return list;
	}


2. , 号  拆分数组

public static List<string> SplitStringDouhao(string str)
	{
		List<string> result = new List<string>();
		if (string.IsNullOrEmpty(str))
		{
			return result;
		}

		string[] tmp = str.Split(',');
		result.AddRange(tmp);
		for (int i = result.Count - 1; i >= 0; i--) {
			if(string.IsNullOrEmpty(result[i]))
			{
				result.RemoveAt(i);
			}
		}
		return result;
	}
3., 号拆分双层数组

1,2,3,;2,3,4 拆分

public static List<List<string>> SplitStringDouhaoAndFenhao(string str)
    {
        List<List<string>> result = new List<List<string>>();
        if (string.IsNullOrEmpty(str))
        {
            return result;
        }

        string[] tmp = str.Split(';');
        for (int i = 0; i < tmp.Length; i++)
        {
            string[] zifu = tmp[i].Split(',');
            List<string> tmpList = new List<string>();
            tmpList.AddRange(zifu);
            for (int j = tmpList.Count - 1; j >= 0; j--)
            {
                if (string.IsNullOrEmpty(tmpList[j]))
                {
                    tmpList.RemoveAt(j);
                }
            }
            result.Add(tmpList);
        }
        return result;
    }

3.  把大于10万的数转换成万为单位

	// 把大于10万的数转换成万为单位
	public static string ConvertNumToWan(int num)
	{
		string result = num.ToString();
		if(num >= 1000000)
		{
			num /= 10000;
			result = num + TextManger.GetInstance().getTextById("txt_num_wan");
		}

		return result;
	}

public static ItemNumData GetItemNumDataByStr(string str)
	{
		List<ItemNumData> datas = GetItemNumDataListByStr(str);
		if(datas.Count > 0)
		{
			return datas[0];
		}

		return null;
	}

public static List<int> GetIntListDataByStr(string str)
	{
		List<int> list = new List<int>();
		list.Clear();
		if (!string.IsNullOrEmpty(str) && !string.Equals(str, "nil"))
		{
			string[] words = str.Split(',');
			for (int i = 0; i < words.Length; i++)
			{
				int tmp = 0;
				if(int.TryParse(words[i], out tmp))
				{
					list.Add(tmp);
				}
			}
		}
		
		return list;
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值