C#字符串处理之分割字符串并保留分隔符
//需要分割的字符串
private string logicFormula="!SW1||SW2&&SW3";
//用于分割的特殊字符
private string[] m_delims = new string[] { "&&", "||", "!", "==" };
//想要的效果
方法如下:
if (!string.IsNullOrEmpty(logicFormula))
{
for (int i = 0; i < m_delims.Length; i++)
{
logicFormula = logicFormula.Replace(m_delims[i], "@" + m_delims[i] + "@");
}
string[] arr = Regex.Split(logicFormula, "@", RegexOptions.IgnoreCase);
foreach (var item in arr)
{
Debug.Log(item);
}
}
记得最后去掉空的