Wednesday 18 June 2014

join (CharSequence delimiter, CharSequence... elements)

public static String join(CharSequence delimiter, CharSequence... elements)
Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

import static java.lang.String.join;

class StringJoin{
 public static void main(String args[]){
  String str;
  str = join("-", "123", "234", "345", "456");
  
  System.out.println("str = " + str);
 }
}

Output
str = 123-234-345-456

1. Throws NullPointerException If delimiter or elements is null.

import static java.lang.String.join;

class StringJoinNullPointer{
 public static void main(String args[]){
  String str;
  str = join(null, "123", "234", "345");
  
  System.out.println("str = " + str);
 }
}

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.Objects.requireNonNull(Objects.java:203)
        at java.lang.String.join(String.java:2438)
        at StringJoinNullPointer.main(StringJoinNullPointer.java:6)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment