[SalesForce] Splitting a String

General Information Requested                     Sample                                                Priority                                    Low                                     Issue Description                       Working                                     Issue Type                      Change                                                  Module                                  english                                     Reported By                     1234                                        Submitted By                        8433                                        Date submited                       12/04/2016

I need to split the sting below showing list format

 General Information Requested
Sample           

Priority

Low

Issue Description

Working

Issue Type

Change

Module

english

Reported By

1234

Submitted By

8433

Date submited

12/04/2016

I am trying to get the result but not getting it. How can I split the string properly?

Best Answer

You can split on a regular expression. In this case, you want to split on any instance of two or more whitespace or &nbsp characters.

String input = 'General Information Requested  ...';
List<String> lines = input.split('(\\s|&nbsp;){2,}');

Components of the above expression:

  • (...){N,} - Match N or more consecutive matches against the expression inside the parentheses (...).
  • A|B - Match expression A or expression B.
  • \\s - Ends up as the expression \s, which matches any whitespace character.
  • &nbsp; - Matches the literal string for a non-breaking space character.