我真的很想搞定这项工作。
我有一个string mess
我需要分成三个块。我已经正确地完成了这项工作但现在我仍然坚持如何验证最后i_clob1
,i_clob2
中包含特殊字符的字符串的两个第一个块("<"
和">"
) },"</"
- 因为当我剪切这三个块时,我的存储过程会附加三个字符串,当它们被剪切掉时会显得很糟糕。
如果第一个(i_clob1
)或第二个(i_clob2
)块包含最后的字符,我想知道如何扩展字符串以便我没有字符结束还是修剪它?没有丢失角色。然后字符将在下一个字符串中。最后一个块(i_clob3
)不需要验证,因为它总是有最后一段文本。
我真的希望有人能回答我的谜语: - )
我的快乐schenario是用一些随机文本而不是那些字符剪切字符串的时候。
我目前的代码:
public void Enqueue(string queueName, string mess)
{
if (mess.Length >= 3 && mess.Length <= 32000 * 3)
{
int lastStart = 2 * mess.Length / 3;
int lastLength = mess.Length - lastStart;
string i_clob1 = mess.Substring(0, (mess.Length / 3));
string i_clob2 = mess.Substring(mess.Length / 3, mess.Length / 3);
string i_clob3 = mess.Substring(lastStart, lastLength);
OracleCommand cmd = null;
try
{
cmd = new OracleCommand("", m_Connection)
{
CommandText = m_InSpName,
CommandType = CommandType.StoredProcedure
};
//add Aq queue name
OracleParameter qName = new OracleParameter("qname", OracleType.VarChar)
{
Direction = ParameterDirection.Input,
Value = queueName
};
//add message to enqueue
OracleParameter message1 = new OracleParameter("i_clob1", OracleType.Clob)
{
Direction = ParameterDirection.Input
};
OracleParameter message2 = new OracleParameter("i_clob2", OracleType.Clob)
{
Direction = ParameterDirection.Input
};
OracleParameter message3 = new OracleParameter("i_clob3", OracleType.Clob)
{
Direction = ParameterDirection.Input
};
i_clob1 = i_clob1.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");
message1.Value = i_clob1;
message2.Value = i_clob2;
message3.Value = i_clob3;
cmd.Parameters.Add(qName);
cmd.Parameters.Add(message1);
cmd.Parameters.Add(message2);
cmd.Parameters.Add(message3);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
//rethrow exception and make sure we clean up i.e. execute finally below
throw new Exception("An error has occurred trying to deliver to the queue", ex);
}
finally
{
if (cmd != null)
{
cmd.Dispose();
}
}
}
}
这是我输入的一个例子。通常它总共约有30.000个字符。
<item>
<item_number>1231</item_number>
<item_title>Lorem ipsum dolor sit</item_title>
<item_pbl_code>Lorem ipsum dolor sit</item_pbl_code>
<item_dep_code>Lorem ipsum dolor sit</item_dep_code>
<item_off_code>Lorem ipsum dolor sit</item_off_code>
<item_digitized_timestamp>2013-11-04 09:07:56</item_digitized_timestamp>
<item_source_url>Loremadsa/adad1231/12312</item_source_url>
<item_cat_code>Lorem ipsum dolor sit</item_cat_code>
<item_ars_code>Lorem ipsum dolor sit</item_ars_code>
<item_ric_code>Lorem ipsum dolor sit</item_ric_code>
<item_rle_code>Lorem ipsum dolor sit</item_rle_code>
<item_code>Lorem ipsum dolor sit</item_code>
<subjects>
<sub_keyword />
</subjects>
<item_description1>A lot of text goes here</item_description1>
<item_description2>A lot of text goes here</item_description2>
<item_description3>A lot of text goes here</item_description3>
</item>
描述字段通常约为10 - 20.000个字符
我的输出将是新的解决方案:
第一块
<item>
<item_number>1231</item_number>
<item_title>Lorem ipsum dolor sit</item_title>
<item_pbl_code>Lorem ipsum dolor sit</item_pbl_code>
<item_dep_code>Lorem ipsum dolor sit</item_dep_code>
<item_off_code>Lorem ipsum dolor sit</item_off_code>
<item_digitized_timestamp>2013-11-04 09:07:56</item_digitized_timestamp>
<item_source_url>Loremadsa/adad
第二块
1231/12312</item_source_url>
<item_cat_code>Lorem ipsum dolor sit</item_cat_code>
<item_ars_code>Lorem ipsum dolor sit</item_ars_code>
<item_ric_code>Lorem ipsum dolor sit</item_ric_code>
<item_rle_code>Lorem ipsum dolor sit</item_rle_code>
<item_code>Lorem ipsum dolor
第三块
sit</item_code>
<subjects>
<sub_keyword />
</subjects>
<item_description1>A lot of text goes here</item_description1>
<item_description2>A lot of text goes here</item_description2>
<item_description3>A lot of text goes here</item_description3>
</item>
答案 0 :(得分:1)
根据您的描述(以及之前的问题),我了解您希望通过计算块大小和块内容来执行除法;两个限制不能同时考虑,并且在任何情况下,你必须设置一个首选项(例如,如果给定的块包含一个必须“移动”到下一个块的字符,但是这个下一个块已经是最大的大小,该怎么办?)。我理解这个偏好定义如下:
执行上述更正的代码:
string i_clob1 = "anything1 </";
string i_clob2 = "anything2 </";
string i_clob3 = "anything3 </";
string allTogether = i_clob1 + i_clob2 + i_clob3;
int start2 = i_clob1.Length;
int length2 = i_clob2.Length;
int start3 = start2 + length2;
string[] bitsToAvoid = new string[] { "</", "<", ">"};
string i_clob1_out = i_clob1;
foreach (string bit in bitsToAvoid)
{
if (i_clob1_out.Substring(i_clob1_out.Length - bit.Length) == bit)
{
start2 = start2 - bit.Length;
length2 = length2 + bit.Length;
i_clob1_out = allTogether.Substring(0, start2);
break; //Just one wrong bit is assumed to be present
}
}
string i_clob2_out = allTogether.Substring(start2, length2);
foreach (string bit in bitsToAvoid)
{
if (i_clob2_out.Substring(i_clob2_out.Length - bit.Length) == bit)
{
start3 = start3 - bit.Length;
i_clob2_out = allTogether.Substring(start2, start3 - start2);
break; //Just one wrong bit is assumed to be present
}
}
string i_clob3_out = allTogether.Substring(start3);
i_clob1 = i_clob1_out; //"anything1 "
i_clob2 = i_clob2_out; //"</anything2"
i_clob3 = i_clob3_out; //"</anything3 </"
注意:此答案具有相对适应性(bitsToAvoid
可以根据需要更新尽可能多的元素),尽管在一定限度内:它是在阅读了问题的原始描述后创建的,其中列出的是人物被转介。如果打算以更复杂的方式定义“要避免的位”(例如,确保给定的闭合节点标签存在与否),则应将此算法视为一个起点,并且必须明显改进(最有可能的是,考虑到一些XML分析)。