3

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

How can I do that?

Lesmana
  • 25,663
  • 9
  • 82
  • 87
user496949
  • 83,087
  • 147
  • 309
  • 426

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
  • 25,663
  • 9
  • 82
  • 87
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,786
  • 3
  • 25
  • 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
  • 42,159
  • 20
  • 102
  • 127
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
2
String test =  "test/";
String go = "/go";
String result = test + go.substring(1);
Heiko Rupp
  • 30,426
  • 13
  • 82
  • 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
  • 47,963
  • 16
  • 124
  • 157
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
  • 292,901
  • 67
  • 465
  • 588
-2

Even simpler:

"test/" + "." + "/go"
Ingo
  • 36,037
  • 5
  • 53
  • 100