I need to get this string before my separator, in this case a pipe character ('|').

So if I have this data:

Malcolm Bell Landgraab | EMP-00012 | Consultant
Juliawati Sandra | EMP-00003 | Consultant

I want this output:

['Malcolm Bell Landgraab', 'EMP-00012', 'Consultant']
['Juliawati Sandra', 'EMP-00003', 'Consultant']

How can I accomplish this task?

enter image description here

  • Use someString.split('\\|'). I cleaned up your question for you, they're a lot more accessible as text. – Adrian Larson Sep 28 '16 at 4:00
  • 1
    @AdrianLarson I think that will still provide an output with white spaces for some characters – Mohith Shrivastava Sep 28 '16 at 4:02
  • @MohithShrivastava Your answer was close, you just forgot about the regular expression part of String.split. You could still fix your answer... – sfdcfox Sep 28 '16 at 4:10
  • @sfdcfox Thanks ! Yes I realized it .Looks like eric has it covered before me – Mohith Shrivastava Sep 28 '16 at 4:22
  • @Vikolai - Is that one string with two rows or two separate strings? – Eric Sep 28 '16 at 4:31

I aM splitting it with respect to newline character and | character. This will work for you.

String inputString='Malcolm Bell Landgraab | EMP-00012 | Consultant\nJuliawati Sandra | EMP-00003 | Consultant';
String []rowsData=inputString.split('\n');

for(String individualROw : rowsData){
    String[] rowArray = individualROw.split('\\|');
        System.debug('[\''+rowArray[0].trim()+'\',\''+rowArray[1].trim()+'\',\''+rowArray[2].trim()+'\']\n' );

If you only want record of Employee only Print rowArray[1] and you are good to go.


This will split it and remove whitespace:

String initialString = 'Malcolm Bell Landgraab | EMP-00012 | Consultant';
String[] results = initialString.trim().split('\\s*\\|\\s*');

Of course you will want to add in all the usual checks for null, errors, etc.

For a single string this will get you the EMP part as requested in your comments:

Note I am not the best at regex so best I can do at this point:

Pattern MyPattern = Pattern.compile('(?<=EMP)([^\\s\\|]*)(?=[$\\s\\|])');
Matcher MyMatcher = MyPattern.matcher(initialString);
Boolean result = MyMatcher.find();
String emp;
    emp = 'EMP' + MyMatcher.group(0);
  • thanks, but how if i just want to get the EMP-00012 only? @eric – Vikolai Sep 28 '16 at 4:24
  • @Vikolai - That is a different question. It would just be a matter of using the right regex expression and has nothing to do with your original question – Eric Sep 28 '16 at 4:26
  • so i need to post one more question? with my point is to ask how to get string based on pipe separator '|' ? @eric what i want achieve is comparing EMP-00012 with my other field, so i just need to get EMP-00012 – Vikolai Sep 28 '16 at 4:29
  • @Vikolai - See updated answer. Note only for a single string. If multiple string you will need to split them and iterate over the individual ones – Eric Sep 28 '16 at 4:44
  • @Vikolai - Curious, where in you question did you actually state that all you wanted was the "EMP-xxx' part cause I do not see it in any form? In the future if you want a question answered you actually need to put it in the question that you post. It is very difficult to read into questions when they are posted as text – Eric Sep 28 '16 at 5:01

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.