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

tangible.DotNetToJavaStringHelper Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package tangible;

//----------------------------------------------------------------------------------------
//	Copyright  2007 - 2012 Tangible Software Solutions Inc.
//	This class can be used by anyone provided that the copyright notice remains intact.
//
//	This class is used to simulate some .NET string functions in Java.
//----------------------------------------------------------------------------------------
public final class DotNetToJavaStringHelper {
    //------------------------------------------------------------------------------------
    //	This method replaces the .NET static string method 'IsNullOrEmpty'.
    //------------------------------------------------------------------------------------
    public static boolean isNullOrEmpty(String string) {
        return string == null || string.equals("");
    }

    public static boolean isNull(String string) {
        return string == null;
    }

    //------------------------------------------------------------------------------------
    //	This method replaces the .NET static string method 'Join' (2 parameter version).
    //------------------------------------------------------------------------------------
    public static String join(String separator, String[] stringarray) {
        if (stringarray == null)
            return null;
        else
            return join(separator, stringarray, 0, stringarray.length);
    }

    //------------------------------------------------------------------------------------
    //	This method replaces the .NET static string method 'Join' (4 parameter version).
    //------------------------------------------------------------------------------------
    public static String join(String separator, String[] stringarray, int startindex, int count) {
        String result = "";

        if (stringarray == null)
            return null;

        for (int index = startindex; index < stringarray.length && index - startindex < count; index++) {
            if (separator != null && index > startindex)
                result += separator;

            if (stringarray[index] != null)
                result += stringarray[index];
        }

        return result;
    }

    //------------------------------------------------------------------------------------
    //	This method replaces the .NET static string method 'TrimEnd'.
    //------------------------------------------------------------------------------------
    public static String trimEnd(String string, Character... charsToTrim) {
        if (string == null || charsToTrim == null)
            return string;

        int lengthToKeep = string.length();
        for (int index = string.length() - 1; index >= 0; index--) {
            boolean removeChar = false;
            if (charsToTrim.length == 0) {
                if (Character.isWhitespace(string.charAt(index))) {
                    lengthToKeep = index;
                    removeChar = true;
                }
            } else {
                for (int trimCharIndex = 0; trimCharIndex < charsToTrim.length; trimCharIndex++) {
                    if (string.charAt(index) == charsToTrim[trimCharIndex]) {
                        lengthToKeep = index;
                        removeChar = true;
                        break;
                    }
                }
            }
            if (!removeChar)
                break;
        }
        return string.substring(0, lengthToKeep);
    }

    //------------------------------------------------------------------------------------
    //	This method replaces the .NET static string method 'TrimStart'.
    //------------------------------------------------------------------------------------
    public static String trimStart(String string, Character... charsToTrim) {
        if (string == null || charsToTrim == null)
            return string;

        int startingIndex = 0;
        for (int index = 0; index < string.length(); index++) {
            boolean removeChar = false;
            if (charsToTrim.length == 0) {
                if (Character.isWhitespace(string.charAt(index))) {
                    startingIndex = index + 1;
                    removeChar = true;
                }
            } else {
                for (int trimCharIndex = 0; trimCharIndex < charsToTrim.length; trimCharIndex++) {
                    if (string.charAt(index) == charsToTrim[trimCharIndex]) {
                        startingIndex = index + 1;
                        removeChar = true;
                        break;
                    }
                }
            }
            if (!removeChar)
                break;
        }
        return string.substring(startingIndex);
    }

    //------------------------------------------------------------------------------------
    //	This method replaces the .NET static string method 'Trim' when arguments are used.
    //------------------------------------------------------------------------------------
    public static String trim(String string, Character... charsToTrim) {
        return trimEnd(trimStart(string, charsToTrim), charsToTrim);
    }

    //------------------------------------------------------------------------------------
    //	This method is used for string equality comparisons when the option
    //	'Use helper 'stringsEqual' method to handle null strings' is selected
    //	(The Java String 'equals' method can't be called on a null instance).
    //------------------------------------------------------------------------------------
    public static boolean stringsEqual(String s1, String s2) {
        if (s1 == null && s2 == null)
            return true;
        else
            return s1 != null && s1.equals(s2);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy