6

I wonder if it's possible to use the apex public String[] split(String regExp) method to split a string by the newline characters. The string being a csv file created on the Windows platform.

I tried several apex expressions in different forms and shapes, but it either doesn't work for me or it probably leaves the carriage return symbol within the resulting strings (if I try to match a resulting string against another clean string it should match, they don't match).

  • Can you provide more detail about where you are getting your input from, possibly including an example? – Adrian Larson Feb 16 '16 at 17:19
  • @AdrianLarson I'm getting the input from the CSV file created with Microsoft Excel. – Andrew Feb 16 '16 at 17:22
  • 1
    You can use \\n or \\r\\n to split in windows – Anurag Bhardwaj Feb 16 '16 at 17:25
  • Did you ever get this working? – Adrian Larson Mar 9 '16 at 21:15
6

Try \n:

myInput.split('\n');

If your input uses \r\n, that is another common option.

myInput.split('\r\n');
  • I have already tried those two. – Andrew Feb 16 '16 at 17:18
  • It accepts a regex, which in apex needs an extra escape. E.g. \\r\\n – ca_peterson Feb 17 '16 at 4:07
  • @ca_peterson Yeah that's what I thought too, but if I debug 'a\nb\nc\nd'.split('\n') I get a list with the correct 4 elements. Why does that work? – Adrian Larson Feb 17 '16 at 4:14
  • Good question, I'm curious myself why this works. My understanding is that it shouldn't, as Andrejs observed. – ca_peterson Feb 17 '16 at 4:24
4

In windows you can use split method in other format. Use below example

variable.split('\\n');

or

variable.split('\\r\\n');

Explanation:

Since Salesforce implements Java Pattern class, the meta-characters that have a special meaning are needs to be escaped. Meta <([{\^-=$!|]})?*+.>.

Here three back slashes are used instead of one is because the meta-character needs to be escaped with a "\" but since "\" is also a meta-character, you need to escape it again with another "\".

Useful Links: Java Regex Tutorial | String.split Blogpost

  • Since you're the first one to realize it...might be worth explaining why you need to escape \ characters. – Adrian Larson Feb 16 '16 at 17:59
  • 2
    And this is not just for windows, btw. – Sebastian Kessel Feb 16 '16 at 18:41
  • In windows, backslash character () is know as escape sequence. Windows use the file system in backslash character. So whenever we need to use any combination of backslash, we will put extra backslash so that window system can understand it. – Anurag Bhardwaj Feb 16 '16 at 18:44
  • Triple backslash doesn't work for me either. But I think that the core issue here is that I should not use the public String[] split(String regExp) method when reading and processing a file, as the behavior may be unpredictable. – Andrew Feb 17 '16 at 9:17
  • But is there any other way? – Andrew Feb 17 '16 at 9:53
0

Note if the string object is too large you will get the exception Regex too complicated. Your regex is actually fine, but you need to truncate the string. In this case you can do something like the following:

Blob blobValue = (Blob)record.get(blobField);
// Truncate string then split on newline, limiting to 11 entries
List<String> preview = blobValue.toString().substring(0,1000).split('\n', 11);
// Remove the last entry, because The list’s last entry contains all input beyond the last matched delimiter.
preview.remove(preview.size()-1);
// In my use-case, I needed to return a string, and String.join() works as the reverse of split()    
return String.join(preview, '\n');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.