-6

I'm messing arround with regular expressions and cutting of malencoded characters in the beginning of a string. I came accross to different implementations:

String Str = new String(".,,¨B<?xml version='1.0' encoding='UTF-8'?> 
str.replaceFirst("(.*)<?xml","<?xml");
str.trim().replaceFirst("(.*)<?xml","<?xml")

The output stays the same. What is the difference here and which one should be used?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Any1
  • 182
  • 2
  • 15
  • why do I get a downvote for asking a legit question? – Any1 Nov 07 '15 at 12:13
  • 1
    Probably because it would take you two seconds to search what trim does. – Matsemann Nov 07 '15 at 12:14
  • 1
    If you hover over the downvote button the text reads "This question does not show any research effort". Your question does not. You could google for all the methods in that code and find out yourself. – takendarkk Nov 07 '15 at 12:15
  • Here you can't see the difference in outputs. Because `trim()` just cuts whitespaces at the begining and at the end of a string. – Igor Tyulkanov Nov 07 '15 at 12:16
  • 1
    @Matsemann I wouldn't be so quick on judging. While googling would tell him what `trim()` does, a newcomer to OO programming might not understand sequential method calls like `methodA().methodB()`, and might think it's a third, different method, from either `methodA()` or `methodB()` independently. --- Read his question: *"difference between `replaceFirst()` and `trim().replaceFirst()`"* --- It's not *"difference between `replaceFirst()` and `trim()`"* – CosmicGiant Nov 07 '15 at 12:19
  • @takendarkk Same as my comment to Mat. Alerting you separately because of the annoying `@` limit. – CosmicGiant Nov 07 '15 at 12:20
  • 1
    @TheLima I didn't say the reason was a good one (I haven't downvoted), just explained why people do – Matsemann Nov 07 '15 at 12:22

4 Answers4

2

String.trim() method remove trailing and leading white spaces from the string. but since the string you are testing doesn't have any trailing or leading white spaces, the below two will certainly return the same string.

str.replaceFirst("(.*)<?xml","<?xml");
str.trim().replaceFirst("(.*)<?xml","<?xml")

However, your regular expression only removes leading white spaces, so if the testing string has trailing white spaces the result would be different. For example:

String str = new String("   .,,¨B<?xml version='1.0' encoding='UTF-8'?>  ");
    String str1 = str.replaceFirst("(.*)<?xml", "<?xml");
    String str2 = str.trim().replaceFirst("(.*)<?xml", "<?xml");

    System.out.println(str1 + "|");
    System.out.println(str2 + "|");

Give you, note the first result still has trailing white spaces.

<?xml version='1.0' encoding='UTF-8'?>  |
<?xml version='1.0' encoding='UTF-8'?>|
Bon
  • 3,073
  • 5
  • 21
  • 40
1

The trim() method removes leading and trailing whitespace. In your case, the difference becomes that it removes trailing whitespace, since your replacement regex will match any characters (including whitespace) preceding <?xml.

BTW, you should change the regex to ".*?<\\?xml" for the following reasons:

  1. You have to escape the ?, otherwise it has the special meaning of making the < optional. So your regex would match "hello xml abc", returning "<?xml abc". Not what you intended.
  2. You have to make the preceding expression non-greedy (or "reluctant"), which is done by changing .* into .*?. Try with an input str of "abc <?xml def <?xml ghi" and you'll see the difference.
  3. The parentheses were just unnecessary. You could keep them if you like.
Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32
0

There is no difference...trim() returns a String, which you are then calling replaceFirst() upon in sequence; it's just like calling String.replaceFirst() directly.

As for the methods' functions, trim() will remove (trim) needless whitespace characters at the edges (beginning and end) of the String.

CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
0

Firstly, you have to understand two method:

  1. trim() -> return a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
  2. replaceFirst(String regex,String replacement) -> Replaces the first substring of this string that matches the given regular expression with the given replacement.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("===========replaceFirst=============");
    String test1 = "I am a teacher in a secondary school";
    String out1 = test1.replaceFirst("a", "an");
    System.out.println(out1);
    System.out.println("===========trim.replaceFirst=============");
    String test2 = "I am a teacher in a secondary school";
    String out2 = test2.trim().replaceFirst("a", "an");
    System.out.println(out2);
}

Output console:

===========replaceFirst=============
I anm a teacher in a secondary school
===========trim.replaceFirst=============
I anm a teacher in a secondary school

So, there is no difference between replaceFirst() and trim().replaceFirst().

Tom
  • 16,842
  • 17
  • 45
  • 54
Hoang Subin
  • 6,610
  • 6
  • 37
  • 56
  • If you want to show that the output of a program is, then it is better to copy it from your console, instead of writing it again. This avoids typos. – Tom Nov 08 '15 at 06:22