Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join an array of strings in Apex

Using Apex, I want to split a string and then rejoin it with the 'AND' operator as the separator.

I split the string successfully but having an issue in rejoining it.

 String [] ideaSearchText = searchText.Split(' ');
 // How to rejoin the array of strings with 'AND'?

How can I do this?

like image 216
Kp Gupta Avatar asked Feb 27 '12 12:02

Kp Gupta


People also ask

How do I join a list of strings in Apex?

You can do this as of v26 (Winter 13) by passing your String[] to String. join() .

How do I return an array from a String in Apex?

The Apex class must have a public static method called 'generateStringArray'. The 'generateStringArray' method must return an array (or list) of strings. Each string must have a value in the format 'Test n' where n is the index of the current string in the array.


1 Answers

You can do this as of v26 (Winter 13) by passing your String[] to String.join().

String input = 'valueOne valueTwo valueThree';
String[] values = input.split(' ');
String result = String.join( values, ' AND ' );

Anonymous Apex output calling System.debug(result):

21:02:32.039 (39470000)|EXECUTION_STARTED
21:02:32.039 (39485000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
21:02:32.040 (40123000)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>()
21:02:32.040 (40157000)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>()
21:02:32.040 (40580000)|USER_DEBUG|[5]|DEBUG|valueOne AND valueTwo AND valueThree
like image 53
doublesharp Avatar answered Sep 20 '22 14:09

doublesharp