Akshay Akshay - 1 year ago 109
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.


Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download