定义一个数据类吧 - 正常根据项目而定
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;
}