分解消息的最有效方法 [英] Most efficient way to break message apart
问题描述
我正在处理HL7消息.示例如下:
I'm dealing with an HL7 message. Sample looks like this:
MSH|^~\&|EPIC|EPICADT|SMS|SMSADT|199912271408|CHARRIS|ADT^A04|1817457|D|2.5|
PID||0493575^^^2^ID 1|454721||DOE^JOHN^^^^|DOE^JOHN^^^^|19480203|M||B|254 MYSTREET AVE^^MYTOWN^OH^44123^USA||(216)123-4567|||M|NON|400003403~1129086|
NK1||ROE^MARIE^^^^|SPO||(216)123-4567||EC|||||||||||||||||||||||||||
PV1||O|168 ~219~C~PMA^^^^^^^^^||||277^ALLEN MYLASTNAME^BONNIE^^^^|||||||||| ||2688684|||||||||||||||||||||||||199912271408||||||002376853
每行的前3个字母是细分名称.接下来是由管道分隔的字段,然后是由插入符号分隔的子字段
The first 3 letters of each line is the segment name. Next are fields separated by pipes, then subfields separated by carets
我事先不知道给定段中可能有多少个字段或子字段.我想要一种将它们分开的方法,以便最终,如果我知道我需要的数据映射,就可以直接将其拔出
I don't know beforehand how many fields or subfields might be in a given segment. I want a method to break these apart so that ultimately if I know the mapping of the data I need I can just pull it directly out
因此,如果我知道姓氏在PID段中,字段4,子字段0,我希望能够做到这一点
So if I know last name is in the PID segment, field 4, subfield 0 I want to be able to do this
string lname = GetData("PID",4,0);
string lname = GetData("PID",4,0);
所以问题是将其分解并存储此信息的最佳方法是什么.我本打算使用多维数组,但似乎没有一种方便的方法来动态设置字段或子字段部分的大小.
So the question is what is the best way to break it apart and store this information. I was going to use a multidimensional array but there doesn't seem to be a convenient method to dynamically set the size for the fields or subfields portion.
预先感谢
车辙
Mike Rutledge
Mike Rutledge
推荐答案
我会将字符串拆分为List
的列表.本质上是具有通用列表的好处的多维数组.
I would split the string into an List of List<string>. Essentially a multidimentional array with the benefits of an generic List.
示例:
private void SplitString(string StrInput)
{
//Create list of list of string to hold results
List<List<string>> LstMain = new List<List<string>>();
//Split by pipe
string[] StrPipeSplit = StrInput.Split('|');
//Loop through and add/split sub strings
for (int i = 0; i < StrPipeSplit.Length; i++)
{
//If contains a caret, it needs to be split again.
if (StrPipeSplit[i].Contains('^'))
{
//Split string into string[] array, cast to List<string>, and add to LstMain all in one step
LstMain.Add(new List<string>(StrPipeSplit[i].Split('^')));
}
//No split needed, add to LstMain
else
{
List<string> LstTemp = new List<string>();
LstTemp.Add(StrPipeSplit[i]);
LstMain.Add(LstTemp);
}
}
}
该函数通过管道分割输入字符串,从而得到一个string []数组.然后,它循环遍历,看看每个字符串项是否都有子项(以插入号表示).如果不是,则将其添加为新的List< string>.只有一件.如果是这样,请拆分它,然后创建 一个新的数组列表,并将其添加到主列表中.
The function splits the input string by pipe, which gives a string[] array. It then loops through and see if each string item has subitems (indicated by a caret). If not, add it as a new List<string> with only one item. If it does, split it, create a new array list, and add that to the main list.
这篇关于分解消息的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!