我想在类似如下的字符串中将用[]括起来的字符串提取出来,得到一个字符串数组,请教各位要如何才能实现?
多谢!字符串:单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核.希望得到如下的数组:
[[单据头.单据编号],[单据头.业务日期],[单据头.制单人]]
多谢!字符串:单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核.希望得到如下的数组:
[[单据头.单据编号],[单据头.业务日期],[单据头.制单人]]
如果没有固定的长度也不是很好取
string s = "";
List<string> list = new List<string>();
string source = "单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核";
for (int i = 0; i < source.Length; i++)
{
if (source[i] == "[") { s = ""; ignore = true; continue; }
if (source[i] == "]") { list.Add(s); s = ""; ignore = false; continue; }
if (source[i] == "," && (!ignore)) { list.Add(s); s = ""; continue; }
s += source[i];
}
string[] array = list.ToArray();
string s = "";
List<string> list = new List<string>();
string source = "单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核";
for (int i = 0; i < source.Length; i++)
{
if (source[i] == "[") { s = ""; ignore = true; continue; }
if (source[i] == "]") { list.Add(s); s = ""; ignore = false; continue; }
if (ignore) s += source[i];
}
string[][] array = list.Select(x => x.Split('.').ToArray()).ToArray();
public static void RunSnippet()
{
string str = "单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核";
StringBuilder sb = new StringBuilder();
for(int i=0; i<str.Length; i++)
{
if(str[i] == '[')
{
sb.Append(str.Substring(i, str.IndexOf(']', i) - i + 1)).Append(',');
}
}
sb.ToString().Split(',');
}
string str = "单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核";
StringBuilder sb = new StringBuilder("[");
foreach (Match match in Regex.Matches(str, @"\[[^]]*?\]"))
{
sb.Append(match.Value + ",");
}
sb.Remove(sb.Length - 1, 1);
sb.Append("]");
Console.WriteLine(sb.ToString());
int startIndex = 0;
int endIndex = 0;
int length = str.Split('[').Length;
string[] Answer = new string[length];
for (int i = 0; i < str.Split('[').Length; i++)
{
startIndex=str.IndexOf("[");
endIndex= str.IndexOf("]")-str.IndexOf("[")+1;
Answer[i] = str.Substring(str.IndexOf("["), endIndex);
str.Remove(startIndex, endIndex);
} Answer 数组 是不是你想要的
Regex regex = new Regex(@"[^\]]+?(?=\[|$)");
str = regex.Replace(str, "");
string[] result = Regex.Split(str, @"(?<!^)(?=\[)");
str = "[" + string.Join(",", result) + "]";
Response.Write(str);