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'?>|
1
solved difference between replaceFirst() and trim().replaceFirst() [duplicate]