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

com.daioware.commons.string.StringUtil Maven / Gradle / Ivy

There is a newer version: 3.2.1-RELEASE
Show newest version
package com.daioware.commons.string;
   
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;

import com.daioware.commons.wrapper.WrapperInt;
import com.daioware.math.Number;

/**
 * 
 * @author Diego Olvera
 *
 */
public class StringUtil {
	public static final char A_UPPER_CASE=65;
	public static final char Z_UPPER_CASE=90;
	
	public static final char ZERO=48;
	public static final char NINE=57;
	
	public static final char ASCII_START=33;
	public static final char ASCII_END=126;
	
	public static final int ONLY_LETTERS=1;
	public static final int ONLY_DIGITS=2;
	public static final int DIGITS_AND_LETTERS=3;
	public static final int ANY_CHARACTER=4;
	
	public static final int RFC_SIZE=13;
	//RFC Validations
	public static final int CHAR_FIRST_VALIDATION=4,CHAR_SECOND_VALIDATION=10;  
	
	public static final Pattern pwdPattern = Pattern
				.compile("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,100})");
	
	public static final Pattern mailPattern=
			Pattern.compile("^[_a-z0-9-]+(.[_a-z0-9-]+)*@(\\w)(.[a-z]{2,3})*$");
	
	public static final String REG_EXP_CALENDAR="\\d+-\\d+$";
	public static final String REG_EXP_BI_CALENDAR="\\d+[a|A|b|B]$";

	private static final Pattern validVariableNamePattern=Pattern.compile("^[a-zA-Z_$][a-zA-Z_$0-9]*$");

	public static final Regex regexCalendar=new Regex(REG_EXP_CALENDAR);
	public static final Regex regexBiCalendar=new Regex(REG_EXP_BI_CALENDAR);
		
	public static String getOrEmpty(String s) {
		return getOr(s,null);
	}
	public static String getOr(String s,String defaultValue) {
		return s!=null?s:"";
	}
	public static boolean isValidEMail(String possibleMail) {
		return mailPattern.matcher(possibleMail).matches();
	}
	public static boolean isValidCalendar(String c) {
		return regexCalendar.matches(c) | regexBiCalendar.matches(c);
	}
	public static boolean anagram(ArrayList strings) {
		int i,stringsSize=strings.size();
		ArrayList> stringFrequencies=new ArrayList<>(stringsSize);
		boolean anagram;
		for(String string:strings) {
			stringFrequencies.add(getFrequencies(string));		
		}
		anagram=true;
		for(i=0;i1;
	}
	public static String getCamelCaseStringWithNoSpaces(String str) {
		StringBuilder newString=new StringBuilder();
		char aux;
		int state=0;
		for(int i=0,j=str.length();i getFrequencies(String str){
		HashMap frequencies=new HashMap<>();
		char aux;
		WrapperInt wrapperInt;
		for(int i=0,j=str.length();i fillers){
		str=str.trim();
		ArrayList cadenas=new ArrayList();
		int indiceComa,indiceEspacio,desde=0,hasta;
		int longStr=str.length();
		boolean aunHayPalabrasPorLeer;
		String rellenador;
		int cuantasVecesHastaEsIgualAMenos1=1;
		char aux;
		int cuentaComas;
		if(longStr>=1){
			do{		
				indiceEspacio=str.indexOf(" ",desde);
				indiceComa=str.indexOf(",",desde);
				if(indiceComa!=-1 && indiceEspacio!=-1){
					hasta=Math.min(indiceComa, indiceEspacio);
				}
				else{
					hasta=indiceComa==-1?indiceEspacio:indiceComa;
					if(hasta==-1 && cuantasVecesHastaEsIgualAMenos1==1){
						hasta=longStr;
						cuantasVecesHastaEsIgualAMenos1++;
					}
				}
				aunHayPalabrasPorLeer=hasta!=-1;
				if(aunHayPalabrasPorLeer){
					if(str.charAt(hasta-1)==')'){
						hasta--;
					}
					cadenas.add(str.substring(desde,hasta));
					rellenador="";	
					if(hasta+1"+t+"";
	}
	public static boolean hasDigitsOrLetters(String cadena){
		if(cadena==null)
			return false;
		int i=0,j;
		for(i=0,j=cadena.length();i=1;
	}
	public static boolean hasLettersAndDigits(String cadena){
		if(cadena==null)
			return false;
		int i=0,j;
		int cuentaDigitos,cuentaLetras;
		char aux;
		for(i=cuentaDigitos=cuentaLetras=0,j=cadena.length();i=1 && cuentaLetras>=1;
	}
	
	public static boolean hasNumbersWithSeparator(String cadena,String separador){
		return hasOnlyDigits(cadena.replaceAll(separador, ""));
	}
	public static boolean hasOnlyDigits(String t){
		if(t==null) 
			return false;
		int i=0,j;
		for(i=0,j=t.length();i=1;
	}
	public static boolean hasSpaces(String s){
		return s.indexOf(" ")>=0;
	}
	public static boolean hasWordsAndOptionallyNumbers(String cadena){
		if(cadena==null)
			return false;
		int i=0,j;
		int cuentaDigitos,cuentaLetras;
		char aux;
		for(i=cuentaDigitos=cuentaLetras=0,j=cadena.length();i=0 && cuentaLetras>=1;
	}
	public static boolean isAnagram(String a,String b) {
		ArrayList strings=new ArrayList<>(2);
		strings.add(a);
		strings.add(b);
		return anagram(strings);
	}   
	public static boolean isPalindromic(String str){ 
		str=str.replaceAll("\\s",str);
		for(int left=0,right=str.length()-1;left=1;
		}
		else{
			return false;
		}			
	}
	public static boolean isValidPassword(String p) {
		return pwdPattern.matcher(p).matches();
	}
		
	public static boolean isValidRFC(String r){
		if(r==null)
			return false;
		int longitudRfc=r.length(),caracterPrimeraValidacion,caracterSegundaValidacion;	
		boolean esRfcCompleto=longitudRfc==RFC_SIZE;
		if( esRfcCompleto || longitudRfc==RFC_SIZE-1){
			caracterPrimeraValidacion=esRfcCompleto?
					CHAR_FIRST_VALIDATION:CHAR_FIRST_VALIDATION-1;
			caracterSegundaValidacion=esRfcCompleto?
					CHAR_SECOND_VALIDATION:CHAR_SECOND_VALIDATION-1;	
			if(!isValidName(r.substring(0, caracterPrimeraValidacion))){
				return false;
			}
			if (!hasOnlyDigits(r.substring(
					caracterPrimeraValidacion, caracterSegundaValidacion))) {
				return false;
			}
			return hasDigitsOrLetters(r.substring(caracterSegundaValidacion,longitudRfc));			
		}
		else{
			return false;
		}
	}
	public static boolean isValidSentence(String o){
		if(o==null){
			return false;
		}
		return !o.replaceAll("\\s", "").equals("");
	}
	public static boolean isValidTelephoneWithNumbersAndScores(String t){
		return hasNumbersWithSeparator(t, "-");
	}
	public static boolean isValidUsername(String s) {
		String regex;
		//regex="^\\p{Alpha}+[\\p{Alnum|_}]*$";
		regex="[a-zA-Z_]+[{a-zA-Z}|{0-9}|_]*$";
		return s.matches(regex);
	}
	
	public static String removeSpaces(String s){
		return s.replaceAll("\\s","");
	}
	public static String replace(String str,int[] whichWordsToReplace,String newStrings[]){
		str=str.trim();
		StringBuilder newStr=new StringBuilder();
		int indiceEspacio,indiceComa,desde=0,hasta,longStr=str.length();
		int cuentaComas;
		char aux;
		int cuentaPalabras=0,longIndicePalabras=whichWordsToReplace.length;
		boolean hayOtraPalabra;
		if(longStr>=1){
			do{
				indiceEspacio=str.indexOf(" ",desde);
				indiceComa=str.indexOf(",",desde);
				if(indiceComa!=-1 && indiceEspacio!=-1){
					hasta=Math.min(indiceComa, indiceEspacio);
				}
				else{
					hasta=indiceComa==-1?indiceEspacio:indiceComa;
				}
				newStr.append(newStrings[whichWordsToReplace[cuentaPalabras++]]);
				if(hayOtraPalabra=hasta!=-1){
					//buscar cocatenaciones extras
					cuentaComas=1;
					for(;hasta whichWordsToReplace){
		StringBuilder newStr=new StringBuilder();
		char aux;
		boolean itCanChangeCharacter;
		for(int i=0,j=str.length(),countingWords=1;i splitWords(String text){
		ArrayList words=new ArrayList<>();
        //\\P{L} means is not a unicode code point that has the property "Letter"
		for(String word:text.trim().split("\\P{L}+")) {
			if(word.length()>=1) {
				words.add(word);
			}
		}
		return words;
	}
	public static String stringWithNoAccentedVowels(String string){
		StringBuilder normalString = new StringBuilder(string.length());
        string = Normalizer.normalize(string, Normalizer.Form.NFD);
        char c,auxCharacter;
    	boolean eneMayus;
    	for(int i=0,j=string.length();i'\u007F'?eneMayus?'�':'�':c);     			
    		}   			
    		else if(c<='\u007F'){
                normalString.append(c);
    		}
    	}
		return normalString.toString();
	}
	public static Iterable forEach(String s) {
		return new Iterable() {
			private int current=0;
			private int size=s.length();
			@Override
			public Iterator iterator() {
				return new Iterator() {
					@Override
					public boolean hasNext() {
						return current=1;
	}	
	public static boolean isAllLowerCase(String name) {
		int j=name.length();
		name.toCharArray();
		for(char c:forEach(name)) {
			if(Character.isLetter(c) && !Character.isLowerCase(c)) {
				return false;
			}
			//else keep looking
		}
		return j>=1;
	}	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy