I have a string that looks like,
var str = "C:\\Users\\source\\repos\\InitiateService\\InitiateService\\bin\\Debug\\net6.0\\Microsoft.CodeAnalysis.CSharp.dll"
How should I split based on 2 delimiters such that I only get Microsoft.CodeAnalysis.CSharp
.
Tried below code
str.Split('//', '.');
But it didn't work.
CodePudding user response:
you need
var str = "C:\\Users\\source\\repos\\InitiateService\\InitiateService\\bin\\Debug\\net6.0\\Microsoft.CodeAnalysis.CSharp.dll";
var fname = System.IO.Path.GetFileNameWithoutExtension(str);
Console.WriteLine(str);
Console.WriteLine(fname);
output
C:\Users\source\repos\InitiateService\InitiateService\bin\Debug\net6.0\Microsoft.CodeAnalysis.CSharp.dll
Microsoft.CodeAnalysis.CSharp
CodePudding user response:
This seems to be a fairly simple way to me:
var str = "C:\\Users\\source\\repos\\InitiateService\\InitiateService\\bin\\Debug\\net6.0\\Microsoft.CodeAnalysis.CSharp.dll";
Console.WriteLine(Path.GetFileNameWithoutExtension(str));
That gives:
Microsoft.CodeAnalysis.CSharp
If, for some reason, you're on Linux, and you pass through a path with Windows directory separators, then you can do this:
Console.WriteLine(Path.GetFileNameWithoutExtension(str.Replace('\\', Path.DirectorySeparatorChar)));