All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jsync.utils.StringUtils Maven / Gradle / Ivy

There is a newer version: 1.10.13
Show newest version
/*
 * Copyright (c) 2011-2013 The original author or authors
 * ------------------------------------------------------
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 */
package io.jsync.utils;

import java.util.Iterator;

public class StringUtils {

    private static final String phoneNumber = "^1\\d{10}$|^\\d{10}$";
    private static final String specialPhoneNumber = "\"^(?:(?:\\\\+?1\\\\s*(?:[.-]\\\\s*)?)?(?:\\\\(\\\\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\\\\s*\\\\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\\\\s*(?:[.-]\\\\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\\\\s*(?:[.-]\\\\s*)?([0-9]{4})(?:\\\\s*(?:#|x\\\\.?|ext\\\\.?|extension)\\\\s*(\\\\d+))?$\"";

    private StringUtils() {
    }

    @Deprecated
    public static Boolean IsEmail(String nString) {
        return isEmail(nString);
    }

    public static boolean isEmail(String string) {
        return string.matches("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$");
    }

    public static boolean isPhoneNumber(String string) {
        return isPhoneNumber(string, false);
    }

    public static boolean isPhoneNumber(String string, boolean special) {
        if (special) {
            if (string.matches(specialPhoneNumber)) {
                return true;
            }

        }
        return string.matches(phoneNumber);
    }

    public static String toTitleCase(String nString) {

        StringBuilder mStringBuilder = new StringBuilder();

        boolean nextTitleCase = true;

        for (char c : nString.toCharArray()) {
            if (Character.isSpaceChar(c)) {
                nextTitleCase = true;
            } else if (nextTitleCase) {
                c = Character.toTitleCase(c);
                nextTitleCase = false;
            }

            mStringBuilder.append(c);
        }

        return mStringBuilder.toString();
    }

    public static String join(final Iterable container) {
        return join(container, "");
    }


    public static String join(final Iterable container, final String delimiter) {
        StringBuilder builder = new StringBuilder();
        Iterator it = container.iterator();
        while (it.hasNext()) {
            builder.append(it.next());
            if (it.hasNext())
                builder.append(delimiter);
        }
        return builder.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy