Take the 2-minute tour ×

This question already has an answer here:

I want to combine the strings "test/" and "/go" as "test/go".

How can I do that?

share|improve this question

marked as duplicate by bluish, senia, Ted Hopp, Achrome, skuntsel Jun 4 '13 at 1:05


8 Answers 8

up vote 6 down vote accepted

FilenameUtils.normalize() from Apache Commons IO does what you want.


FilenameUtils.normalize("foo/" + "/bar");

returns the string "foo/bar"

share|improve this answer

Using only java.io.File the easiest way is to do:

String combined_path = new File("test/", "/go").getPath();
share|improve this answer
+1 for this simple solution without extra library although it is limitated to two strings (which is sufficient for most cases and for the question) –  FrVaBe Apr 8 '11 at 7:49
@K. Claszen really? Where in the question are Files mentioned? –  Sean Patrick Floyd Apr 8 '11 at 7:51
@Sean Patrick Floyd You are right. Sorry, my brain sometimes injects content into questions and I can't do anything against it. Taken the question 'as is' we have to go with Heiko Rupp solution. Everything else is overkill. –  FrVaBe Apr 8 '11 at 8:00
@K. Claszen but that assumes that we know the parameters in advance, which would make the whole situation unnecessary –  Sean Patrick Floyd Apr 8 '11 at 8:03
Note that this solution does work even when the paths don't exist. The question topic is Combine two strings in a single string representing a path. –  dancek Apr 8 '11 at 8:07

As suggested by Jon Skeet here

public static String combine (String path1, String path2)
    File file1 = new File(path1);
    File file2 = new File(file1, path2);
    return file2.getPath();
share|improve this answer
Wow, this is quite heavy. –  Heiko Rupp Apr 8 '11 at 8:06
FilenameUtils would be a much better route IMO, but if you want to go this route, you're better of doing something like. new File("/home", "//foobar").getPath(). –  csgeek Feb 21 '13 at 1:28

Append both the strings and replace // with / as below

"test//go".replace("//", "/")

Output: test/go

share|improve this answer
String test =  "test/";
String go = "/go";
String result = test + go.substring(1);
share|improve this answer
+1 for answering the question 'as is' without any further estimations. –  FrVaBe Apr 8 '11 at 8:02

Here's a Guava method that joins an Iterable<String> of items with a char, after trimming all instances of this character from beginning and end of the items:

public static String joinWithChar(final Iterable<String> items,
    final char joinChar){
    final CharMatcher joinCharMatcher = CharMatcher.is(joinChar);
    return Joiner.on('/').join(
        Iterables.transform(items, new Function<String, String>(){

            public String apply(final String input){
                return joinCharMatcher.trimFrom(input);


System.out.println(joinWithChar(Arrays.asList("test/", "/go"), '/'));



This solution

  • is not restricted to file paths, but to any types of Strings
  • will not replace any characters found inside the tokens, only trim them from the boundaries
share|improve this answer

Supposed this is a question related to file names, than take a look at apache-commons-io and its FilenameUtils class

final String test = "test/";
final String go ="/go";
System.out.println(FilenameUtils.normalize(test + go));

output on windows:


The normalize method will do much more for you, e.g. change '/' to '\' on windows systems.

By the way congrat's to your reputation score by nearly just asking questions :-)

share|improve this answer
just asking questions is what you meant probably :-) –  Sean Patrick Floyd Apr 8 '11 at 7:53
@Sean Patrick Floyd That's what I meant! Fixed! –  FrVaBe Apr 8 '11 at 7:58

Even simpler:

"test/" + "." + "/go"
share|improve this answer
