Find first vowel from string and add it to a var

Viewed 196

I want the find the first vowel in a string (apellidoPat) for example if my string it's "CRUZ" my expexted output is : "U" to concatenate in rfc what can I use ? a loop ??

  • rfc : first letter of apellidoPat + first vowel of apellidoPat + irst letter of apellidoMat + date (YYMMDD)

I have all except first vowel of apellidoPat

    private String getRFC() {

       String rfc = "";
       String apellidoPat = "CRUZ";
       String apellidoMat = "HERNANDEZ";
       String nombre = "JAVIER";
       String fechaNac = "1997-12-25";
       
       rfc = apellidoPat.substring(0,1) + "FIST VOWEL apellidoPat" + apellidoMat.substring(0,1) + nombre.substring(0,1) + fechaNac.substring(2,4) + fechaNac.substring(5,7) + fechaNac.substring(8,10);
       // Expected output: CUHJ971225
         
       return rfc

}
4 Answers

To get the first vowel of a string, you can try:

static final String vowels = "aeiou";
int indexOfFirstVowel = indexOfFirstVowel(apellidoPat);

private static int indexOfFirstVowel(String word){
        String loweredWord = word.toLowerCase();
        for (int index=0; index<loweredWord.length(); index++)
        {
            if (vowels.contains(String.valueOf(loweredWord.charAt(index))))
            {
                return index;
            }
        }
        return -1;
    }

Explanation:

  1. indexOfFirstVowel method return the index of the first vowel in case it is found. Otherwise, it will return -1. You can change as per your requirement.

  2. In case of String apellidoPat = "CRUZ"; this method return 2 i.e. index of U.

  3. You can try below code.

        String rfc;
        String apellidoPat = "CRUZ";
        int indexOfFirstVowel = indexOfFirstVowel(apellidoPat);
        String apellidoMat = "HERNANDEZ";
        String nombre = "JAVIER";
        String fechaNac = "1997-12-25";
    
        rfc = apellidoPat.substring(0, 1);
    
        if(indexOfFirstVowel != -1) {
            rfc += apellidoPat.charAt(indexOfFirstVowel);
        }
        rfc += apellidoMat.substring(0,1) + nombre.substring(0,1) + fechaNac.substring(2,4) + fechaNac.substring(5,7) + fechaNac.substring(8,10);
    
        System.out.println(rfc);
    

Find first vowel from string and add it to a var

You can do it by replacing all characters except the vowels with an empty string and get the first character from the replaced string.

public class Main {
    public static void main(String[] args) {
        String apellidoPat = "CRUZ";
        String replacedStr = apellidoPat.replaceAll("[^AEIOUaeiou]", "");
        String firstVowel = "";
        if (!replacedStr.isEmpty()) {
            firstVowel = replacedStr.substring(0, 1);
        }
        System.out.println(firstVowel);
    }
}

Output:

U

Thus, in your case, it will be:

rfc = firstVowel + "FIST VOWEL apellidoPat" + apellidoMat.substring(0,1) + nombre.substring(0,1) + fechaNac.substring(2,4) + fechaNac.substring(5,7) + fechaNac.substring(8,10);

Note: Check this for the explanation of the regex I have used with String#replaceAll.

You do not need to add the date part by part to avoid -, it is better to split it by - and join the parts together to get desired result. And you can use a loop to find first vowel:

private String getRFC(){
    String vowels = "AEIOUaeiou";

    String apellidoPat = "CRUZ";
    String apellidoMat = "HERNANDEZ";
    String fechaNac = "1997-12-25";

    // first letter of apellidoPat
    String rfc = String.valueOf(apellidoPat.charAt(0));

    //  first vowel of apellidoPat
    for (int i = 0; i < apellidoPat.length(); ++i){
        if (vowels.indexOf(apellidoPat.charAt(i)) != -1){
            rfc += String.valueOf(apellidoPat.charAt(i));
            break;
        }
    }

    // first letter of apellidoMat + date (YYMMDD)
    rfc += apellidoMat.charAt(0) + String.join("", fechaNac.split("-"));

    return rfc;
}

you can use a for loop for this intention. for example :

String text = "CRUZ";
    String vawel = "aeiouAEIOU";
    
    for(int i=0 ; i<text.length() ; i++)
    {
        char ch = text.charAt(i);
        
        if(vawel.contains(String.valueOf(ch)))
        {
            //add to your variable
        }
    }
Related