Insert line into line
I would use the Path class , preferably in your own utility method or as an extension method.
string pathWithThumbs = Path.Combine(Path.Combine(Path.GetDirectoryName(path), "thumbs"), Path.GetFileName(path));
Link seems to be out of place here; you are not really asking for collections. In addition, the class Path
handles most of the cuts and corner cases for you automatically.
EDIT: As @juharr pointed out, there is a handy overload as of 4.0 that makes it even easier:
string pathWithThumbs = Path.Combine(Path.GetDirectoryName(path), "thumbs", Path.GetFileName(path));
EDITx2: Hrrrm, as @DiskJunky points out, using this path will actually replace your forward slashes with a backslash, so just challenge Replace("\\", "/")
there.
source to share
For something like manipulating tracks, it is best to use a namespace System.IO
, specifically an object Path
. You can do something like:
string path = "/media/pictures/image1.jpg";
string newPath = Path.Combine(Path.GetDirectoryName(path), "thumbs", Path.GetFileName(path)).Replace(@"\", "/");
source to share
Try this, you will get the index of the last slash and insert an extra line at that point.
Not sure why this is happening, but I can assure you that it works.
string original = "/media/pictures/image1.jpg";
string insert = "thumbs/";
string combined = original.Insert(original.LastIndexOf("/") + 1, insert);
source to share
Is this possible with linq?
You don't need to use Linq for this process. You can use the String.Insert () method ;
Returns a new string in which the specified string is inserted at the specified index position in this case.
string s = "/media/pictures/image1.jpg";
string result = s.Insert(s.LastIndexOf('/'), "/thumbs");
Console.WriteLine(result);
Output;
/media/pictures/thumbs/image1.jpg
Here is DEMO .
source to share
I would use the System.IO class called Path
.
Here is the long version for demo purposes:
string pathToImage = "/media/pictures/image1.jpg";
string dirName = System.IO.Path.GetDirectoryName(pathToImage);
string fileName = System.IO.Path.GetFileName(pathToImage);
string thumbImage = System.IO.Path.Combine(dirName, "thumb", fileName);
Debug.WriteLine("dirName: " + dirName);
Debug.WriteLine("fileName: " + fileName);
Debug.WriteLine("thumbImage: " + thumbImage);
Here's a one-liner:
Debug.WriteLine("ShortHand: " + Path.Combine(Path.GetDirectoryName(pathToImage), "thumb", Path.GetFileName(pathToImage)));
I am getting the following output:
dirName: \media\pictures
fileName: image1.jpg
thumbImage: \media\pictures\thumb\image1.jpg
ShortHand: \media\pictures\thumb\image1.jpg
source to share