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

net.clementlevallois.utils.MyStringUtils Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.clementlevallois.utils;

import java.text.Normalizer;

/**
 *
 * @author LEVALLOIS
 */
public class MyStringUtils {

    /**
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        for (char c : str.toCharArray()) {
            if (!Character.isDigit(c)) {
                return false;
            }
        }
        return true;
    }

    /**
     *
     * @param str
     * @param substr
     * @param n
     * @return
     */
    public static int ordinalIndexOf(String str, String substr, int n) {
        int pos = str.indexOf(substr);
        while (--n > 0 && pos != -1) {
            pos = str.indexOf(substr, pos + 1);
        }
        return pos;
    }

    // from https://stackoverflow.com/a/15191508/798502

    /**
     *
     * @param string
     * @return
     */
    public static String flattenToAscii(String string) {
        char[] out = new char[string.length()];
        string = Normalizer.normalize(string, Normalizer.Form.NFD);
        int j = 0;
        for (int i = 0, n = string.length(); i < n; ++i) {
            char c = string.charAt(i);
            if (c <= '\u007F') {
                out[j++] = c;
            }
        }
        return new String(out,0,j);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy