The issue you are having is with your target sObject field, declared as Text, length 255. These fields don't support new line characters. If you think about it for a moment, text fields are used for Account.name, Opportunity.name and there's no way to enter a new line in such fields on the standard UI.
Fields declared as textArea or textArea (long) do support new line characters
I created a custom object Foo__c
in my DE with both Text (len 255) and TextArea custom fields
Using this code:
insert new Foo__c(name='00foo',
text__c = String.format('{0}\n{1}',new List<String>{'Hello','world'}),
textarea__c = String.format('{0}\n{1}',new List<String>{'Hello','world'})
);
The results are clear - the new line is present in the textArea field but not the Text field:
Now, when you use \n
at the end of the string pattern for format; they are stripped off before being saved in the database:
insert new Foo__c(name='03foo',
text__c = String.format('{0}\n{1}\n',new List<String>{'Hello','world'}),
textarea__c = String.format('{0}\n{1}\n\n\n\n',new List<String>{'Hello','world'})
);
system.debug(loggingLevel.INFO,[select text__c, textarea__c from Foo__c where name = '03foo']);
12:49:51.262 (262880325)|USER_DEBUG|[6]|INFO|(Foo__c:{Text__c=Hello world, Id=a094000000P3BW4AAN, TextArea__c=Hello
world})