James Morrish
James Morrish

Reputation: 475

Remove the end of a string and merge it with another

I have two strings:

  1. C:\folder1\folder2\folder3

  2. folder3\folder4\file1.jpg

I want to combine the two strings to give a full file path, but when using:

char[] charsToTrim = {'\\'};
var rootPathEdit = treeViewPath.TrimStart(charsToTrim);

it doesn't work, can you give me any tips?

EDIT:

Sorry I realised it wasn't very clear, I want the result of combining:

  1. C:\folder1\folder2\folder3

  2. folder3\folder4\file1.jpg

to be: C:\folder1\folder2\folder3\folder4\file1.jpg

because the two half of the file path I have overlap at the end and start.

Upvotes: 0

Views: 211

Answers (3)

Nino
Nino

Reputation: 7115

use Path.Combine for path concatenation.

var finalPath = Path.Combine(firstPath, secondPath);

EDIT:

Since the real problem (not very clearly defined in original post) is duplicate folder, that exist in both paths, here's one way to remove extra folder:

var start = @"C:\folder1\folder2\folder3";
var end = @"folder3\folder4\file1.jpg";

var startArr = start.Split('\\');
var endArr = end.Split('\\');

var duplicateFolders = startArr.Intersect(endArr);
var final = Path.Combine(startArr.Concat(endArr.Except(duplicateFolders)).ToArray());

//... and some fix (because final path doesn't have backslash after :
final = final.Replace(":", @":\");

Upvotes: 3

mjwills
mjwills

Reputation: 23975

Here is one possible avenue to explore.

Basically you split the two strings up by \ and then compare the last element of the first string vs the first element of the last string. If they are the exact same, strip one of them.

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var start = @"C:\folder1\folder2\folder3";
        var end = @"folder3\folder4\file1.jpg";

        var startArray = start.Split('\\');
        var endArray = end.Split('\\');

        var final = Path.Combine(start, end);
        var endOfStart = startArray.LastOrDefault();
        if (endOfStart  == endArray.FirstOrDefault())
        {
            final = Path.Combine(start.Substring(0, start.Length - (endOfStart ?? "").Length), end);
        }

        Console.WriteLine(final);
        Console.ReadLine();

    }
}

Upvotes: 1

Mighty Badaboom
Mighty Badaboom

Reputation: 6155

Don't use string operations for this because there are better ways and string operations are not as fail-safe as the code below.

Use Directory.GetParent to get the parent from the first path (cause you said in the title you want to remove the end of the first string). If you don't want to, skip this part.

var firstPath = Directory.GetParent(@"C:\folder1\folder2\folder3").ToString();
var secondPath = @"folder3\folder4\file1.jpg";

Then use Path.Combine to combine both paths.

var result = Path.Combine(firstPath, secondPath);

or when one of the paths is a relative one you should use

var finalResult = Path.GetFullPath(result);

as well.

Upvotes: 2

Related Questions