如何切出路径的一部分? [英] How to cut out a part of a path?
本文介绍了如何切出路径的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我想切出路径的一部分,但不知道如何。
获取路径,我使用这段代码:
Hello guys
I want to cut out a part of a path but don't know how.
To get the path, I use this code:
String path = System.IO.Path.GetDirectoryName(fullyQualifiedName);
(路径=Y:\\Test \\Project \\\\\Debug)
现在我需要第一部分没有\\\\\Debug。
如何切割这部分走出目前的路径?
我很感谢所有答案。
问候
Epanjohura
(path = "Y:\\Test\\Project\\bin\\Debug")
Now I need the first part without "\\bin\\Debug".
How can I cut this part out of the current path?
I'm thankful for all answers.
Greetings
Epanjohura
推荐答案
你没有提到你是否想要它在C ++或C#中。但是你的编码模式似乎我在C#中。另一个问题,你的上述路径是静态的吗?
如果是这样,那么你只能在3行内完成。
You don't mention whether you want it in C++ or C#. But your coding pattern seems me it is in C#. Another question,is your above path static?
If so then you can do it within only 3 lines.
String path= @"Y:\\Test\\Project\\bin\\Debug";
String[] extract = Regex.Split(path,"bin"); //split it in bin
String main = extract[0].TrimEnd('\\'); //extract[0] is Y:\\Test\\Project\\ ,so exclude \\ here
Console.WriteLine("Main Path: "+main);//get main path
你的标签有点令人费解,因为你标记了C#和MFC,它们不能很好地协同工作。到目前为止你得到的答案与C#有关。如果您使用的是MFC,那意味着C ++,您可以使用 _splitpath( )/ _ wsplitpath() [ ^ ]然后 std :: string [ ^ ]或 CString [ ^ ]提取或替换部分路径。
CString适用于ANSI或Unicode,std :: string仅适用于ANSI。他们都有自己的声音自以为是的宗教游说。
Your tags are a bit puzzling since you tagged C# and MFC, which don't play well together. The answers you have so far are related to C#. If you are using MFC, that means C++, and you can use _splitpath()/_wsplitpath()[^] and then either std::string[^] or CString[^] to extract or replace parts of the path.
CString works with ANSI or Unicode, std::string is ANSI only. Both have their own vocal self righteous religious lobbies.
你不能修改任何字符串;字符串是 immutable ,因此更改字符串的所有字符串方法实际上都会创建一个新的字符串对象。您可以使用以下内容: http://msdn.microsoft.com/en-us/library/fk49wtc1。 aspx [ ^ ]。
-SA
You cannot modify any string; strings are immutable, so all string methods changing the string actually create a new string object. You can use this one: http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx[^].
—SA
这篇关于如何切出路径的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文