tRuEsAtM
tRuEsAtM

Reputation: 3668

How to get part of the string based on 2 delimiters?

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.

Upvotes: 0

Views: 58

Answers (2)

Enigmativity
Enigmativity

Reputation: 117064

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

output

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)));

Upvotes: 1

pm100
pm100

Reputation: 50190

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

  

Upvotes: 0

Related Questions