惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - wolfman

正则表达式Pattern ActiveMQ在C#中的应用 深入掌握JMS(十二):MDB 深入掌握JMS(十一):TemporaryQueue和TemporaryTopic 深入掌握JMS(十):JMSCorrelationID与Selector 深入掌握JMS(九):Selector 深入掌握JMS(八):JMSReplyTo 深入掌握JMS(七):DeliveryMode例子 深入掌握JMS(六):消息头 深入掌握JMS(四):实战Queue 深入掌握JMS(五):实战Topic 深入掌握JMS(三):MessageListener 深入掌握JMS(二):一个JMS例子 深入掌握JMS(一):JSM基础 oracle 分区表(转) Application,Session,Cookie,ViewState,Cache的区别(转) 网络编程基础 VS2008+Oracle92 网站发布注意问题 Windows Service开发应用
字符串分割成一维数组、二维数组,一维数组与二维数组之间的转换
wolfman · 2011-06-25 · via 博客园 - wolfman

指定格式的字符串截成一维数组(二维数组)的操作类

做项目时经常会遇到将"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 "";

一维数组和二维数组的转换表示(C#)

关键:下标的计算

一维->二维

一个n个元素的一维数组,转换为r行c列的二维数组

对于一维数组中任意一个元素的下标i(0 <= i < n)

其对应的二维数组下标为 (i / c, i % c), 显然, 只与列数c有关,而与行数r无关

code like this

int[] a = new int[n];
int[,] b = new int[r, c];for (int i = 0; i < n; i++)
    b[i 
/ c, i % c] = a[i];

also can write like this

for (int i = 0; i < r; i++)
{
    
for (int j = 0; j < c; j++)
    {
        b[i, j] 
= a[i * c + j];
    }
}

二维->一维

int[,] a = new int[r, c];
int[] b = new int[r * c];
for(int i = 0; i < b.Length; i++)
    b[i] 
= a[i / c, i % c];