Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.List;

public class Main {
    /**
     * Join the {@code strings} into one string.
     * 
     * @param strings the string list to join
     * 
     * @return the joined string
     */
    public static String join(List<String> strings) {
        if (strings == null) {
            throw new NullPointerException("argument 'strings' cannot be null");
        }
        return join(strings.toArray(new String[strings.size()]));
    }

    /**
     * Join the {@code strings} into one string with {@code delimiter} in 
     * between.
     * 
     * @param strings the string list to join
     * @param delimiter the delimiter
     * 
     * @return the joined string
     */
    public static String join(List<String> strings, String delimiter) {
        if (strings == null) {
            throw new NullPointerException("argument 'strings' cannot be null");
        }
        return join(strings.toArray(new String[strings.size()]), delimiter);
    }

    /**
     * Join the {@code strings} into one string.
     * 
     * @param strings the string list to join
     * 
     * @return the joined string
     */
    public static String join(String[] strings) {
        return join(strings, null);
    }

    /**
     * Join the {@code strings} into one string with {@code delimiter} in 
     * between. It is similar to RegExpObject.test(string) in JavaScript.
     * 
     * @param strings the string list to join
     * @param delimiter the delimiter
     * 
     * @return the joined string
     */
    public static String join(String[] strings, String delimiter) {
        if (strings == null) {
            throw new NullPointerException("argument 'strings' cannot be null");
        }

        StringBuilder sb = new StringBuilder();

        if (strings.length != 0) {
            sb.append(strings[0]);
            for (int i = 1, iEnd = strings.length; i < iEnd; i++) {
                if (delimiter != null) {
                    sb.append(delimiter);
                }
                sb.append(strings[i]);
            }
        }

        return sb.toString();
    }
}