javaapache-commons-lang

Stringutils.join Alternative Way


Are there any alternative way of using the following? I am using Apache commons.lang jar for StringUtils.join however I do have a problem with class loading to Weblogic server and hence it wouldn't be good to use StringUtils.join.

So instead I am looking alternative ways of achieving the following

String pattern = "\\b(" + StringUtils.join(tokens, "|")+"\\b";

Solution

  • Java 1.8:

     String pattern = "\\b(" + String.join("|", tokens)+")";
    

    Or, before that:

     StringBuffer sb = new StringBuffer( "\\b(" );
     String del = "";
     for( String t: tokens ){
         sb.append( del ).append( t );
         del = "|";
     }
     pattern = sb.toString();