Akshay Akshay - 29 days ago 20
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.

Thanks

Answer

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