使用Apex,我想分割一个字符串,然后以'AND'运算符作为分隔符重新加入它。
我成功地拆分了字符串,但重新加入了它的问题。
String [] ideaSearchText = searchText.Split(' ');
// How to rejoin the array of strings with 'AND'?
我该怎么做?
答案 0 :(得分:26)
您可以将String[]
传递给String.join()
,从v26(冬季13)开始执行此操作。
String input = 'valueOne valueTwo valueThree';
String[] values = input.split(' ');
String result = String.join( values, ' AND ' );
Anonymous Apex输出调用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
Salesforce API文档:http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_string.htm
答案 1 :(得分:0)
请注意,如果字符串对象太大,则会出现Regex too complicated
异常。在这种情况下,您可以执行以下操作:
Blob blobValue = (Blob)record.get(blobField);
// Truncate string then split on newline, limiting to 11 entries
List<String> preview = blobValue.toString().substring(0,1000).split('\n', 11);
// Remove the last entry, because The list’s last entry contains all
// input beyond the last matched delimiter.
preview.remove(preview.size()-1);
// In my use-case, I needed to return a string, and String.join() works
// as the reverse of split()
return String.join(preview, '\n');