C# 指定格式的字符串截成一维数组(二维数组)的操作类
指定格式的字符串截成一维数组(二维数组)的操作类
做项目时经常会遇到将"1,3,a,b,d"截成一维数组或将"1,a;2,b;3,c;4,d"截成二维数组。虽然String.Split()可以实现,但我感觉在遍历取值时还不是很方便。
所以写下clsArrayList类,专门做这方面的处理。
注:myClass.clsLogHelper 为写错误日志类,在我的博客中有写,有兴趣的朋友可能看一下。
using System;
using System.Text;
namespace myClass
{
class clsArrayList
{
public int p_Count=-1; //数据组数
private string[] myArray1; //一维矩阵
private string[,] myArray2; //二维矩阵
/// <summary>
/// 一维字符串分隔,初始化.例: clsArrayList myArray1List=new clsArrayList("1;2;3;4;5",';')
/// </summary>
/// <param name="strStringSource"></param>
/// <remarks>gx 2010-04-25</remarks>
public clsArrayList(string strStringSource, char charSepartor)
{
try
{
myArray1 = strStringSource.Split(charSepartor);//截取
p_Count = myArray1.Length;
}
catch(Exception ex)
{
myClass.clsLogHelper.m_CreateErrorLogTxt("clsArrayList(" + strStringSource + " , " + charSepartor + ")", "", ex.Message.ToString());
}
}
/// <summary>
/// 二维字符串分隔,初始化. 例: clsArrayList myArray1List=new clsArrayList("1,A;2,B;3,C;4,D;5,E" , ';' , ',')
/// </summary>
/// <param name="strStringSource"></param>
/// <param name="strColumnSepartor"></param>
/// <param name="strRowSepartor"></param>
/// <remarks>gx 2010-04-25</remarks>
public clsArrayList(string strStringSource, char charColumnSepartor, char charRowSepartor)
{
try
{
string[] strColumn; //第一次截取 ";"
string[] strRow; //第二次截取 ","
strColumn = strStringSource.Split(charColumnSepartor);//第一次截取
p_Count = strColumn.Length;
myArray2 = new string[p_Count, 2];
for (int i = 0; i <= p_Count - 1; i++) //将数据保存 数组 myArray1 中
{
strRow = strColumn[i].Split(charRowSepartor);//第二次截取
myArray2[i, 0] = strRow[0];
myArray2[i, 1] = strRow[1];
}
}
catch (Exception ex)
{
myClass.clsLogHelper.m_CreateErrorLogTxt("clsArrayList(" + strStringSource + " , " + charColumnSepartor + " , " + charRowSepartor + ")", "", ex.Message.ToString());
}
}
/// <summary>
/// 一维字符串,取值
/// </summary>
/// <param name="intIndex"></param>
/// <returns></returns>
public string m_GetItem(int intIndex)
{
if (myArray1 == null)
{
return "";
}
if (p_Count == - 1)
{
return "";
}
if (intIndex > p_Count - 1)
{
return "";
}
else
{
return myArray1[intIndex];
}
}
/// <summary>
/// 二维字符串,取值
/// </summary>
/// <param name="intKeyIndex"></param>
/// <param name="intValueIndex"></param>
/// <returns></returns>
/// <remarks>gx 2010-04-25</remarks>
public string m_GetItem(int intKeyIndex,int intValueIndex)
{
if (myArray2 == null)
{
return "";
}
if (p_Count == -1)
{
return "";
}
if (intKeyIndex > p_Count - 1 || intValueIndex > 1)//Value为列,二维
{
return "";
}
else
{
return myArray2[intKeyIndex, intValueIndex];
}
}
/// <summary>
/// 二维字符串,通过Value 获取Key ,例: m_GetKey("1")
/// </summary>
/// <param name="strValue"></param>
/// <returns></returns>
/// <remarks>gx 2010-04-25</remarks>
public string m_GetKey(string strValue)
{
try
{
if (myArray2 == null)
{
return "";
}
if (p_Count == -1)
{
return "";
}
for (int i = 0; i <= p_Count - 1; i++) //通过value查找key
{
if (myArray2[i, 1] == strValue)
{
return myArray2[i, 0]; //返回key的值
}
}
return "";
}
catch(Exception ex)
{
myClass.clsLogHelper.m_CreateErrorLogTxt("clsArrayList(" + strValue + ")", "", ex.Message.ToString());
return "";
}
}
/// <summary>
/// 二维字符串,通过Key 获取Value
/// </summary>
/// <param name="strKey"></param>
/// <returns></returns>
/// <remarks>gx 2010-04-25</remarks>
public string m_GetValue(string strKey)
{
try
{
if (myArray2 == null)
{
return "";
}
if (p_Count == -1)
{
return "";
}
for (int i = 0; i <= p_Count - 1; i++) //通过key查找value
{
if (myArray2[i, 0] == strKey)
{
return myArray2[i, 1]; //返回value的值
}
}
return "";
}
catch(Exception ex)
{
myClass.clsLogHelper.m_CreateErrorLogTxt("m_GetValue(" + strKey + ")", "", ex.Message.ToString());
return "";
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)