// 通过字段名,字段中出现次数获得字段内容
// 次数从 1 开始计数
// -1 error
// 0 no found
// 1 found
public int GetField(string strFldName,
int nIndex,
out string strFld)
{
strFld = null;
string strFldPos = null;
int nRet = this.GetFieldPos(strFldName,nIndex,out strFldPos);
if (nRet != 1)
return nRet;
if(strFldName.Length != 3 )
return -1; // subfield must 3 chars
int nLength = int.Parse( strFldPos.Substring(3,4));
int nBeginPos = int.Parse( strFldPos.Substring(7,5));
char[] chData = this.GetData().ToCharArray();
int nPos =0;
int i = 0;
while( nPos < chData.Length)
{
i += GetCharLength(chData[nPos]);
if((i >= nBeginPos) && i<= (nBeginPos + nLength))
strFld += chData[nPos].ToString();
nPos ++;
}
if(strFld == null)
return 0;
return 1;
}
//从字段中获得出现次数的子字段
// -1 error
// 0 not found
// 1 found
public int GetSubField(string strFld,
string strSubName,
int nIndex,
out string strSubFld)
{
strSubFld = null;
if(strSubName.Length != 1)
return -1; // subfield'symbol char must 1 char
if(strFld == null)
return -1;
char[] chData = strFld.ToCharArray();
int nPos = 0;
bool isNewSub = false;
int nFound = 0; // 0: not 1: first time found 2: second time found
while( nPos < chData.Length)
{
nPos ++;
if((chData[nPos-1] == SUBFLD) && (chData[nPos].ToString() == strSubName))
nFound ++; // found
if ((nFound == nIndex) && (isNewSub == false))
{
if(chData[nPos] == SUBFLD)
{
isNewSub = true;
break;
}
strSubFld += chData[nPos].ToString();
}
}
if(strSubFld == null)
return 0;
return 1;
}
//从字段组中获得子字段
// -1 error
// 0 not found
// 1 found
public int GetSubField(string strGroup,
string strSubName,
out string strSubFld)
{
strSubFld = null;
if(strSubName.Length != 1)
return -1; // subfield'symbol char must 1 char
if(strGroup == null)
return -1;
char[] chData = strGroup.ToCharArray();
int nPos = 0;
bool isNewSub = false;
int nFound = 0; // 0: not 1: first time found 2: second time found
while( nPos < chData.Length)
{
nPos ++;
if((chData[nPos-1] == SUBFLD) && (chData[nPos].ToString() == strSubName))
nFound ++; // found
if (isNewSub == false)
{
if(chData[nPos] == SUBFLD)
{
isNewSub = true;
break;
}
strSubFld += chData[nPos].ToString();
}