如何获取字符串的子字符串,如file_ {AAA} _ {BBB} .xml?
我的代码:
string[] result = Regex.Split(str, "{(.*)}"); result.ToList().ForEach(x => MessageBox.Show(x));
结果:
AAA
BBB
答案 0 :(得分:1)
你可以试试这个:
string str = "file_{AAA}_{BBB}.xml";
var regex = new Regex("(?<=\{)[^}]*(?=\})");
var matches = regex.Matches(str);
答案 1 :(得分:0)
超级贫民区,我吮吸正则表达式而你应该使用Rahul的答案,但是:
string f = "file_{AAA}_{BBB}.xml";
string o = String.Empty;
while (f.Contains('{') && f.Contains('}'))
{
int openIndex = f.IndexOf('{');
int closeIndex = f.IndexOf('}');
o += f.Substring(openIndex + 1, closeIndex - openIndex - 1) + " ";
f = f.Remove(0, closeIndex + 1);
}
o.Trim();
Console.WriteLine(o);
将输出AAA BBB
答案 2 :(得分:0)
来自Rahul Tripathi的原文,刚刚删除{}
string str = "file_{AAA}_{BBB}.xml";
var regex = new Regex("{.*?}");
var matches = regex.Matches(str);
List<string> result = new List<string>();
foreach (var item in matches)
{
result.Add(item.ToString().Replace("{", "").Replace("}", ""));
}
答案 3 :(得分:-1)
您可以遵循:
string filename = string.Format(@&#34; {0}&#34;,Path.GetFileName(fileUpload1.PostedFile.FileName));
string filepath =&#34; f:\ ClientDocument \&#34; + Path.GetFileName(fileUpload1.PostedFile.FileName);
int fileLength =(fileUpload1.PostedFile.ContentLength)/ 1024;