2

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";
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • You can also use the Guava API for that see answer of http://stackoverflow.com/questions/1751844/java-convert-liststring-to-a-joind-string – GandalfIX Jan 29 '15 at 11:40

1 Answers1

7

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();
Carlos Verdes
  • 3,037
  • 22
  • 20
laune
  • 31,114
  • 3
  • 29
  • 42
  • I recommend to use String.format instead of concat strings with +, something like: String pattern=String.format("\\b(%s)",String.join("|",tokens)); – Carlos Verdes Jan 29 '15 at 11:48
  • 1
    @CarlosVerdes how is String.format better? (as it is certainly slower) – Peter Lawrey Jan 29 '15 at 11:50
  • I have edited my question, I haven't closed `\b`, if that is the case how do I close \b? – Jacob Jan 29 '15 at 11:52
  • Concat is faster but I prefer format because the code is cleaner (it's my opinion) and because the + for Strings in Java is not a good practice (when you concat 2 strings java creates 3 instances in memory, one per String and another for the concatenated one, so String.format or StringBuilder is a better approach from my point of view) – Carlos Verdes Jan 29 '15 at 11:55
  • 1
    @Peter Lawrey See.. Polppan missed to closed \b... I think if he had something like String pattern= "\\b(%s)\\b";... then he would have noticed before. If you are not calling String.format millions per second and need a really high performance, I would recommend String.format really (but, like I said, it's just my opinion). – Carlos Verdes Jan 29 '15 at 11:59
  • @Carlos Verdes, the compiler will change trivial string concatenations to string builder operations. So using string concats shouldn't be a problem. – Kishore Jan 29 '15 at 12:01
  • @CarlosVerdes Could you provide an example with `String.format`? – Jacob Jan 29 '15 at 12:02
  • Agree with the compiler part, but I still think that String.format is a better practice, the code is easier to understand. – Carlos Verdes Jan 29 '15 at 12:03
  • @Polppan i already gave an example in my first comment, just create one String with the pattern you want to create, where %s would be the tokens. Something like protected static final String PATTERN_EXPR= "\\b(%s)"; .. then you can do something like String pattern= String.format(PATTERN_EXPR,String.join("|", tokens)). – Carlos Verdes Jan 29 '15 at 12:06
  • @CarlosVerdes What about closing `\b` ? – Jacob Jan 29 '15 at 12:07
  • @Polppan what do you mean closing \b? I the tokens are "asd", "qwe", what is your expected result? – Carlos Verdes Jan 29 '15 at 12:10