Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
11 kB
2
Indexable
Never
public class StringManipulation {

    /**
     * Returns the length of the input string.
     *
     * @param str The input string
     * @return The length of the input string
     */
    public static int length(String str) {
        int count = 0;
        for (char c : str.toCharArray()) {
            count++;
        }
        return count;
    }

    /**
     * Concatenates two strings together.
     *
     * @param str1 The first string
     * @param str2 The second string
     * @return The concatenated string
     */
    public static String concat(String str1, String str2) {
        char[] result = new char[length(str1) + length(str2)];
        int index = 0;
        for (char c : str1.toCharArray()) {
            result[index] = c;
            index++;
        }
        for (char c : str2.toCharArray()) {
            result[index] = c;
            index++;
        }
        return new String(result);
    }

    /**
     * Returns a substring of the input string.
     *
     * @param str        The input string
     * @param beginIndex The starting index of the substring (inclusive)
     * @param endIndex   The ending index of the substring (exclusive)
     * @return The substring of the input string
     */
    public static String substring(String str, int beginIndex, int endIndex) {
        StringBuilder result = new StringBuilder();
        int count = 0;
        for (char c : str.toCharArray()) {
            if (count >= beginIndex && count < endIndex) {
                result.append(c);
            }
            count++;
        }
        return result.toString();
    }

    /**
     * Checks if the input string is empty or not.
     *
     * @param str The input string
     * @return True if the input string is empty, false otherwise
     */
    public static boolean isEmpty(String str) {
        return length(str) == 0;
    }

    /**
     * Replaces all occurrences of a specified old character with a new character in the input string.
     *
     * @param str     The input string
     * @param oldChar The old character to be replaced
     * @param newChar The new character to replace the old character with
     * @return The replaced string
     */
    public static String replace(String str, char oldChar, char newChar) {
        StringBuilder result = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (c == oldChar) {
                result.append(newChar);
            } else {
                result.append(c);
            }
        }
        return result.toString();
    }

    /**
     * Splits the input string into an array of substrings using the specified delimiter.
     *
     * @param str       The input string
     * @param delimiter The delimiter to split the string
     * @return An array of substrings
     */
    public static String[] split(String str, String delimiter) {
        int count = 0;
        for (int i = 0; i < length(str) - length(delimiter) + 1; i++) {
            if (substring(str, i, i + length(delimiter)).equals(delimiter)) {
                count++;
            }
        }
        String[] result = new String[count + 1];
        int index = 0;
        int startIndex = 0;
        int endIndex = 0;
        while (endIndex < length(str)) {
            if (substring(str, endIndex, endIndex + length(delimiter)).equals(delimiter)) {
                result[index] = substring(str, startIndex, endIndex);
                index++;
                startIndex = endIndex + length(delimiter);
                endIndex = startIndex;
            } else {
                endIndex++;
            }
        }
        result[index] = substring(str, startIndex, endIndex);
        return result;
    }

    /**
     * Joins the elements of the input array into a single string using the specified delimiter.
     *
     * @param arr       The input array
     * @param delimiter The delimiter to join the elements
     * @return The joined string
     */
    public static String join(String[] arr, String delimiter) {
        if (arr.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(arr[0]);
        for (int i = 1; i < arr.length; i++) {
            sb.append(delimiter).append(arr[i]);
        }
        return sb.toString();
    }

    /**
     * Checks if two strings are equal regardless of the case sensitivity.
     *
     * @param str1 The first string
     * @param str2 The second string
     * @return True if the two strings are equal, false otherwise
     */
    public static boolean equalsIgnoreCase(String str1, String str2) {
        if (str1 == null || str2 == null) {
            return str1 == str2;
        }
        if (str1.length() != str2.length()) {
            return false;
        }
        for (int i = 0; i < str1.length(); i++) {
            char c1 = str1.charAt(i);
            char c2 = str2.charAt(i);
            if (c1 != c2 && Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns the index of the first occurrence of a specified character in the input string.
     *
     * @param str The input string
     * @param ch  The character to be searched for
     * @return The index of the first occurrence of the character, -1 if not found
     */
    public static int indexOf(String str, char ch) {
        for (int i = 0; i < length(str); i++) {
            if (str.charAt(i) == ch) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Formats a string using the specified format and arguments.
     *
     * @param format The format string
     * @param args   The arguments to be substituted in the format string
     * @return The formatted string
     */
    public static String format(String format, Object... args) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        int argIndex = 0;
        while (i < format.length()) {
            char c = format.charAt(i);
            if (c == '{') {
                if (i + 2 >= format.length() || format.charAt(i + 2) != '}') {
                    throw new IllegalArgumentException("invalid format");
                }
                char c2 = format.charAt(i + 1);
                if (c2 == '0') {
                    String str = args[argIndex++] == null ? "null" : args[argIndex - 1].toString();
                    sb.append(str);
                    i += 3;
                } else if (c2 == '1') {
                    int number = args[argIndex++] == null ? 0 : ((Number) args[argIndex - 1]).intValue();
                    sb.append(number);
                    i += 3;
                } else {
                    throw new IllegalArgumentException("invalid format");
                }
            } else {
                sb.append(c);
                i++;
            }
        }
        return sb.toString();
    }

    /**
     * Checks if the input string starts with the specified prefix.
     *
     * @param str    The input string
     * @param prefix The prefix to be checked
     * @return True if the input string starts with the prefix, false otherwise
     */
    public static boolean startsWith(String str, String prefix) {
        if (length(prefix) > length(str)) {
            return false;
        }
        for (int i = 0; i < length(prefix); i++) {
            if (str.charAt(i) != prefix.charAt(i)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Checks if the input string ends with the specified suffix.
     *
     * @param str    The input string
     * @param suffix The suffix to be checked
     * @return True if the input string ends with the suffix, false otherwise
     */
    public static boolean endsWith(String str, String suffix) {
        if (length(suffix) > length(str)) {
            return false;
        }
        int strIndex = length(str) - 1;
        int suffixIndex = length(suffix) - 1;
        while (suffixIndex >= 0) {
            if (str.charAt(strIndex) != suffix.charAt(suffixIndex)) {
                return false;
            }
            suffixIndex--;
            strIndex--;
        }
        return true;
    }

    /**
     * Converts the input string to lowercase.
     *
     * @param str The input string
     * @return The lowercase string
     */
    public static String toLowercase(String str) {
        StringBuilder result = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (c >= 'A' && c <= 'Z') {
                result.append((char) (c - 'A' + 'a'));
            } else {
                result.append(c);
            }
        }
        return result.toString();
    }


    /**
     * Converts the input string to uppercase.
     *
     * @param str The input string
     * @return The uppercase string
     */
    public static String toUppercase(String str) {
        StringBuilder result = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (c >= 'a' && c <= 'z') {
                result.append((char) (c - 'a' + 'A'));
            } else {
                result.append(c);
            }
        }
        return result.toString();
    }

    /**
     * Removes leading and trailing whitespaces from the input string.
     *
     * @param str The input string
     * @return The trimmed string
     */
    public static String trim(String str) {
        int startIndex = 0;
        while (startIndex < length(str) && str.charAt(startIndex) == ' ') {
            startIndex++;
        }
        int endIndex = length(str) - 1;
        while (endIndex >= 0 && str.charAt(endIndex) == ' ') {
            endIndex--;
        }
        return substring(str, startIndex, endIndex + 1);
    }

    /**
     * Compares two strings lexicographically.
     *
     * @param str1 The first string
     * @param str2 The second string
     * @return The value 0 if the two strings are equal; a value less than 0 if the first string is lexicographically less
     * than the second string; a value greater than 0 if the first string is lexicographically greater than the second
     * string
     */
    public static int compareTo(String str1, String str2) {
        int len1 = length(str1);
        int len2 = length(str2);
        int lim = Math.min(len1, len2);
        for (int k = 0; k < lim; k++) {
            char c1 = str1.charAt(k);
            char c2 = str2.charAt(k);
            if (c1 != c2) {
                return c1 - c2;
            }
        }
        return len1 - len2;
    }
}
Leave a Comment