我有一个输入字符串:
"SQLSELECT SELECT location_id, department_name "Department" FROM tttt UNION SQLSELECT SELECT location_id, warehouse_name FROM warehouses EXCEPT SQLSELECT SELECT location_id, department_name "Department" FROM XYZ UNION ALL SQLSELECT SELECT location_id, warehouse_name FROM PQR INTERSECT SQLSELECT SELECT location_id, warehouse_name FROM ABC".
我想要这种格式的输出字符串:
"UNION
SQLSELECT SELECT location_id, department_name "Department" FROM tttt
EXCEPT
SQLSELECT SELECT location_id, warehouse_name FROM warehouses
UNION ALL
SQLSELECT SELECT location_id, department_name "Department" FROM XYZ
INTERSECT
SQLSELECT SELECT location_id, warehouse_name FROM PQR
SQLSELECT SELECT location_id, warehouse_name FROM ABC"
如果我首先找到union,然后用union分割行,那么我想递归地执行它,即
" SQLSELECT SELECT location_id,department_name" Department" FROM tttt UNION SQLSELECT SELECT location_id,warehouse_name FROM warehouses EXCEPT SQLSELECT SELECT location_id,department_name" Department"来自XYZ"
然后应该在数组中添加 " SQLSELECT SELECT location_id,department_name" Department"从tttt UNION"
然后对于下一个循环,如果找不到则检查union,然后检查EXCEPT并拆分字符串并添加
" SQLSELECT SELECT location_id,warehouse_name FROM warehouses EXCEPT"在数组中 等等。
我想最终输出就像
" UNION
SQLSELECT SELECT location_id,department_name" Department"来自tttt
除了
SQLSELECT SELECT location_id,warehouse_name FROM仓库
UNION ALL
SQLSELECT SELECT location_id,department_name" Department"来自XYZ
相交
SQLSELECT SELECT location_id,warehouse_name FROM PQR
SQLSELECT SELECT location_id,warehouse_name FROM ABC"
我的代码是:
if (sTempLine.Trim().ToUpper().StartsWith("SQLSELECT") && (sTempLine.Trim().ToUpper().Contains("UNION") || sTempLine.Trim().ToUpper().Contains("UNION ALL") || sTempLine.Trim().ToUpper().Contains("INTERSECT") || sTempLine.Trim().ToUpper().Contains("EXCEPT") || sTempLine.Trim().ToUpper().Contains("MINUS")))
{
index = sTempLine.ToUpper().Trim().IndexOf("UNION");
//sTempLine = sTempLine.Trim().Substring(0,index);
for (int i=0; i < sTempLine.Length; i++)
{
index = sTempLine.ToUpper().IndexOf("UNION");
if (sTempLine.Contains("UNION"))
{
if (index > 0)
{
listInput.Add(sTempLine.Substring(0, index + 5));
listInput.Add(sTempLine.Substring(index + 5));
sTempLine = sTempLine.Substring(index + 5);
continue;
}
}
if (sTempLine.Contains("UNION ALL"))
{
if (index > 0)
{
listInput.Add(sTempLine.Substring(0, index + 9));
listInput.Add(sTempLine.Substring(index + 9));
sTempLine = sTempLine.Substring(index + 9);
continue;
}
}
if (sTempLine.Contains("INTERSECT"))
{
if (index > 0)
{
listInput.Add(sTempLine.Substring(0, index + 9));
listInput.Add(sTempLine.Substring(index + 9));
sTempLine = sTempLine.Substring(index + 9);
continue;
}
}
if (sTempLine.Contains("EXCEPT"))
{
if (index > 0)
{
listInput.Add(sTempLine.Substring(0, index + 6));
listInput.Add(sTempLine.Substring(index + 6));
sTempLine = sTempLine.Substring(index + 6);
continue;
}
}
if (sTempLine.Contains("MINUS"))
{
if (index > 0)
{
listInput.Add(sTempLine.Substring(0, index + 5));
listInput.Add(sTempLine.Substring(index + 5));
sTempLine = sTempLine.Substring(index + 5);
continue;
}
}
}
}
else
{
}
sbFileWrite.AppendLine(sTempLine + ClsGlobal.Space_Const+ sLineNumber);
}
答案 0 :(得分:0)
string input = "SASASA EXCEPT ASASA UNION"; // test input here is your input string which is a sql query
int UnionIndex = input.IndexOf("UNION");
int ExceptIndex = input.IndexOf("EXCEPT");
List<string> SplitArray = new List<string>(); // here is the list with the sub strings
if (UnionIndex < ExceptIndex)
{
SplitArray = input.Split(new[] { "UNION" },
StringSplitOptions.RemoveEmptyEntries).ToList();
}
else if (ExceptIndex < UnionIndex)
{
SplitArray= input.Split(new[] { "EXCEPT" },
StringSplitOptions.RemoveEmptyEntries).ToList();
}