1
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?

4

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.
|improve this answer|||||
1

In this example I am using ; you can change that to whatever you are using to separate your string values.

string example = 'one;two;three';
List<String> list = new List<String>();
for(String s : example.split(';'))
{
   list.add(s);
}
|improve this answer|||||
  • 1
    No need for the loop – Eric Sep 27 '16 at 15:12
  • I provided an example where he could figure out the logic for his case. I know the loop is not needed but for someone who is starting it makes more sense. I am just trying to help here. He can always mark your answer as correct and if you don't like mine... go ahead and delete it. – Carlos Sep 27 '16 at 15:17

Your Answer

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