-6

Consider this code:

string xx = "/test/file2/".Trim('/');
Console.WriteLine(xx);
Console.Read();

This code returns test/file2. What is an efficient way to do this in Java?

Luke101
  • 59,479
  • 80
  • 216
  • 348
  • 8
    I'd suggest you do away with the C# comparison, and just ask how to trim a character from either end of the String. People looking at the c# tag probably don't care about this question (unless they also happen to look at the Java tag), and people looking at the java tag probably don't care that c# has this ability, they just care on what it is you're trying to accomplish. – yshavit Sep 25 '17 at 17:49

1 Answers1

1

Here is how to do the same in Java:

 String xx = "/test/file2/".replaceAll("(^/*|/*$)", "");

This uses a regex that matches multiple / from the beginning or multiple / at the end and replaces them with void.

Alexandre Fenyo
  • 4,030
  • 1
  • 12
  • 23