Akshay Akshay - 8 months ago 81
C# Question

Retrieve String Containing Specific substring C#

I am having an output in string format like following :

"ABCDED 0000A1.txt PQRSNT 12345"

I want to retreieve substring(s) having .txt in above string. e.g. For above it should return 0000A1.txt.



Split will work if it the spaces are the seperator. if you use oter seperators you can add as needed

 string input = "ABCDED 0000A1.txt PQRSNT 12345";
 string filename = input.Split(' ').FirstOrDefault(f => System.IO.Path.HasExtension(f));

filname = "0000A1.txt" and this will work for any extension