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?

0

2 Answers 2

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.
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);
}
2
  • 2
    No need for the loop
    – Eric
    Commented Sep 27, 2016 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
    Commented Sep 27, 2016 at 15:17

You must log in to answer this question.