3

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

How can I do that?

Lesmana
  • 24,114
  • 8
  • 78
  • 84
user496949
  • 79,431
  • 144
  • 301
  • 419

8 Answers8

11

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

String combined_path = new File("test/", "/go").getPath();
  • 1
    +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 08 '11 at 07:49
  • @K. Claszen really? Where in the question are Files mentioned? – Sean Patrick Floyd Apr 08 '11 at 07: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 08 '11 at 08:00
  • @K. Claszen but that assumes that we know the parameters in advance, which would make the whole situation unnecessary – Sean Patrick Floyd Apr 08 '11 at 08: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*. – StackExchange saddens dancek Apr 08 '11 at 08:07
6

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

example:

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

returns the string "foo/bar"

Lesmana
  • 24,114
  • 8
  • 78
  • 84
5

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();
}
Community
  • 1
  • 1
Babar
  • 2,766
  • 3
  • 26
  • 35
  • 1
    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 01:28
4

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

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

Output: test/go

Simon Nickerson
  • 40,547
  • 20
  • 99
  • 126
GuruKulki
  • 25,118
  • 47
  • 138
  • 197
2
String test =  "test/";
String go = "/go";
String result = test + go.substring(1);
Heiko Rupp
  • 29,382
  • 13
  • 81
  • 119
1

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:

test\go

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

FrVaBe
  • 46,188
  • 15
  • 116
  • 152
1

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>(){

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

Usage:

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

Output:

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
Sean Patrick Floyd
  • 284,665
  • 62
  • 456
  • 576
-2

Even simpler:

"test/" + "." + "/go"
Ingo
  • 35,584
  • 5
  • 51
  • 97