![](/img/trans.png)
[英]C# Reading strings from a file and finding matches between Start string and an End String different by one character
[英]How to get string between start and end character in C#
如何獲取像 file_{AAA}_{BBB}.xml 這樣的字符串的子串?
我的代碼:
string[] result = Regex.Split(str, "{(.*)}"); result.ToList().ForEach(x => MessageBox.Show(x));
結果 :
AAA
BBB
你可以試試這個:
string str = "file_{AAA}_{BBB}.xml";
var regex = new Regex("(?<=\{)[^}]*(?=\})");
var matches = regex.Matches(str);
超級貧民窟,我不擅長正則表達式,您可能應該改用 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
來自 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("}", ""));
}
你可以遵循:
string filename = string.Format(@"{0}", Path.GetFileName(fileUpload1.PostedFile.FileName));
字符串文件路徑 = "f:\\ClientDocument\\" + Path.GetFileName(fileUpload1.PostedFile.FileName); int fileLength = (fileUpload1.PostedFile.ContentLength) / 1024;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.