-
July 16th, 2009, 11:03 AM
#1
Cutting String
using List<string> to store them.
There is a list of strings as shown below:
"C:\WithCreationFilter_CS\bin\Debug\hello.dll"
"C:\AAA\bin\Debug\XXX.dll"
"C:\ZZZ\bin\Debug\YYY.dll"
"C:\QQQ\XXX\YYY\bin\Debug\xxx.dll"
How to get expected output as shown below??
Expected ouput:
"hello.dll"
"XXX.dll"
"YYY.dll"
"xxx.dll"
-
July 16th, 2009, 11:45 AM
#2
Re: Cutting String
Code:
string s = "C:\\WithCreationFilter_CS\\bin\\Debug\\hello.dll";
string result = s.Remove(s.LastIndexOf("\\"));
-
July 16th, 2009, 12:41 PM
#3
Re: Cutting String
Originally Posted by dannystommen
Code:
string s = "C:\\WithCreationFilter_CS\\bin\\Debug\\hello.dll";
string result = s.Remove(s.LastIndexOf("\\"));
I think you meant SubString instead of Remove. Your code returns "C:\WithCreationFilter_CS\bin\Debug"
-
July 16th, 2009, 01:32 PM
#4
Re: Cutting String
All he needs is the file name to be returned from the full path. How about trying Path.GetFileName() method? Something like this
Code:
string FileName = Path.GetFileName(@"C:\WithCreationFilter_CS\bin\Debug\hello.dll" );
-
July 16th, 2009, 01:59 PM
#5
Re: Cutting String
Originally Posted by Shuja Ali
All he needs is the file name to be returned from the full path. How about trying Path.GetFileName() method? Something like this
Code:
string FileName = Path.GetFileName(@"C:\WithCreationFilter_CS\bin\Debug\hello.dll" );
This is the way to go as it also checks for invalid characters and other edge cases.
-
July 17th, 2009, 07:14 AM
#6
Re: Cutting String
So complete sample could look like:
Code:
var paths = new List<string>
{
"C:\WithCreationFilter_CS\bin\Debug\hello.dll",
"C:\AAA\bin\Debug\XXX.dll" ,
"C:\ZZZ\bin\Debug\YYY.dll" ,
"C:\QQQ\XXX\YYY\bin\Debug\xxx.dll"
}
var fileNames = paths.Select(p => Path.GetFileName(p)).ToList();
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
July 17th, 2009, 07:51 AM
#7
Re: Cutting String
It seems I answered this question yesterday on another forum. Don't you love cross posters?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|