3

I'm having troubles insert a substring in a string what I want is to inject "/thumbs" into a stringpath

/media/pictures/image1.jpg

I want to inject /thumbs/ into the last part of the path like this:

/media/pictures/thumbs/image1.jpg

Is it possible with linq?

3
  • 3
    Why would you use linq and not just the String Insert method? Commented Feb 18, 2013 at 20:39
  • Why would you use link and not just the nice Path utility class meant to help do stuff exactly like this? string pathWithThumbs = Path.Combine(Path.Combine(Path.GetDirectoryName(path), "thumbs"), Path.GetFileName(path)); Commented Feb 18, 2013 at 20:39
  • 1
    simple check for the last / (or first from the end) and then insert /thumbs Commented Feb 18, 2013 at 20:40

5 Answers 5

4

For something like path manipulation, it's best to use the System.IO namespace, specifically the Path object. You can do something like;

string path = "/media/pictures/image1.jpg";
string newPath = Path.Combine(Path.GetDirectoryName(path), "thumbs", Path.GetFileName(path)).Replace(@"\", "/");
Sign up to request clarification or add additional context in comments.

Comments

3

Try this, you get the index of the last forward slash and insert the additional string at that point.

Unsure as to why the downvote, but I assure it works.

string original = "/media/pictures/image1.jpg";
string insert = "thumbs/";
string combined = original.Insert(original.LastIndexOf("/") + 1, insert);

5 Comments

I didn't down vote, but I assume it's the "you should use Path methods instead of string methods" argument.
Most likely, but seems a little bit out of the way for such a simple operation IMO. string.Insert exists for a reason ;)
Yeah, and I'm not sure the Path methods would keep the slashes going the correct way / vs \
It seems silly to use Path.Combine just to replace the slashes afterwards. string.Insert is easier to read as well, no need to over-code it really.
@AdamKDean, well not in production code. The Path object is a little more versitile and something like this would possibly be put into a common library. A bit of a stretch perhaps but a job worth doing... :-) That said, I do like your approach - neat and to the point!
2

Is it possible with linq?

You don't need to use Linq for this process. You can use String.Insert() method;

Returns a new string in which a specified string is inserted at a specified index position in this instance.

string s = "/media/pictures/image1.jpg";
string result = s.Insert(s.LastIndexOf('/'), "/thumbs");
Console.WriteLine(result);

Output;

/media/pictures/thumbs/image1.jpg

Here is a DEMO.

1 Comment

That will give /media/pictures/thumbs//image1.jpg, you need to remove one of the slashes.
1

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

Linq seems to be out of place here; you're not really querying collections. Plus, the Path class handles most of the slashes and corner cases for you automatically.

EDIT: As @juharr pointed out, as of 4.0 there's a handy overload making it even simpler:

string pathWithThumbs = Path.Combine(Path.GetDirectoryName(path), "thumbs", Path.GetFileName(path));

EDITx2: Hrrrm, as @DiskJunky points out, this Path usage will actually swap your forward slashes for backslashes, so just throw a Replace("\\", "/") call in there.

4 Comments

Path.Combine will now take a param array so you don't need to nest them. I think that was added in .Net 4.0.
@juharr Thanks! It's been a long time since I've had to dive into using Path directly; very nice overload!
The result of your solution replaces the "/" with "\" - you need to complete it with a Replace() call at the end of the chain
@DiskJunky +1 Thanks for picking up on that.
1

I would use System.IO class called Path.

Here is the long(er) version for demonstration purposes only:

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 is a one-liner:

Debug.WriteLine("ShortHand: " + Path.Combine(Path.GetDirectoryName(pathToImage), "thumb", Path.GetFileName(pathToImage)));

I get the following output:

dirName: \media\pictures
fileName: image1.jpg
thumbImage: \media\pictures\thumb\image1.jpg

ShortHand: \media\pictures\thumb\image1.jpg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.