Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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);
(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
Posted 19-Jul-13 4:17am
Edited 21-Jul-13 21:35pm
v2
Comments
Sergey Alexandrovich Kryukov at 19-Jul-13 9:21am
   
Why "MFC"? What prevents you from just reading the documentation?
—SA
ThePhantomUpvoter at 19-Jul-13 9:22am
   
Pick a programming language first. The answers you get are probably going to be vastly different. But for either one you chose, using a combination of SubString and IndexOf will probably do the trick.
Sergey Alexandrovich Kryukov at 19-Jul-13 11:25am
   
It's C#, "MFC" could be a slip of a hand. :-)
—SA
ridoy at 19-Jul-13 11:10am
   
do you want it in C# or C++?
epanjohura at 22-Jul-13 2:35am
   
c#, my fault.. but thanks for every help, I solved the problem
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 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
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 19-Jul-13 11:24am
   
It's a good point, even though the question is apparently on .NET, 5ed.
(Ha, someone decided to down-vote both answers. Funny :-)
—SA
Maciej Los at 19-Jul-13 14:35pm
   
Good point!
+5!
epanjohura at 22-Jul-13 2:36am
   
thank you so much for your help
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Maciej Los at 19-Jul-13 9:36am
   
String.Replace function... This is it!
+5!
Sergey Alexandrovich Kryukov at 19-Jul-13 9:43am
   
Thank you, Maciej.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi . i think the simplest way is :
 
string s=path.Substring(path.IndexOf('Y'),15);
in this line of code you get a substring by position of 'Y' character.
and the count of forwarding to get substring, is 15 character.
i hope this help you.
Good luck
  Permalink  
Comments
epanjohura at 23-Jul-13 8:07am
   
I already have a solution but thanks for your help
ali rad at 24-Jul-13 6:49am
   
you're wecolme.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Afzaal Ahmad Zeeshan 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 22 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100