2012-05-23 47 views
0

我有一個文本如下圖所示,如何從文本中提取帶.txt的文件名?

Lorem存有悲坐阿梅德,consectetur sample1.txt adipiscing ELIT。 Morbi nec urnana non ante varius semper eget vitae ipsum。 Pellentesque habitant sample2.txt morbi tristique senectus et netus et malesuada fames。

我已經sample1.txt和上面的文字sample2.txt。名稱因樣本1和樣本2而異。我只需要使用c#獲取文件名。

任何人都可以幫助我嗎?

回答

1

既然你標記它LINQ:

var filesnames = text.Split(new char[] { }) // split on whitespace into words 
        .Where(word => word.EndsWith(".txt")); 
+0

我總是解釋'linq'意味着LINQ提供的標籤,需要不LINQ ;這是荒謬的。作爲程序員,唯一真正重要的是以高效和可維護的方式解決問題。 – mellamokb

+0

@mellamokb:嗯,這對這兩個賬戶來說都是非常好的解決方案。 – jason

-1

它可能會用正則表達式,如果有捕捉到你的文件的名稱將是什麼樣子的好方法。我假設在這裏它總是blah.txt與字母數字字符:

var matches = Regex.Matches(input, @"\b[a-zA-Z0-9]+\.txt\b"); 
0

嘗試是這樣的

var filesnames = text.Split(' ') 
       .Where(o => o.EndsWith(".txt")).Select(o => o.SubString(o.LastIndexOf('.'))).ToList(); 
相關問題