Search code examples
javaarraysstringletter

Given a string/text, how can I take each following pair of letters from the given string/text?


Given a string, how can I take each pair of letters ?

Here is an example, given string = "asdfg", the code outputs arr[as,df,g_]; given string = "asdfgghk", the code outputs arr[as,df,gg,hk].

If the string length is odd then the last pair will be the char[i]_, (underscore)

Here is part of the code I wrote:


    public static char
    getCharFromString(String s, int index) 
    { 
        return s.charAt(index); 
    } 
    public static String[] solution(String s) {
        char a, b;
        String[] arr = new String[(s.length() / 2)];
        for(int i = 0; i < s.length(); i+=2) {
            if(s.length() % 2 == 0) {
                 a = getCharFromString(s, i);
                 b = getCharFromString(s, i + 1);
                arr[i] = a + b ;
            }
            else {
                a = getCharFromString(s, i);
                b = getCharFromString(s, i + 1);
                arr[i] = a + b ;
                if(i == s.length()) {
                    b = "_";
                }   
            }
        }
        return arr;
    }


Solution

  • It is quite easy using a regex. Simply split the string on every 2 characters and if the last element of the array is short of one character, append _ to it.

    Demo:

    import java.util.Arrays;
    
    public class Main {
        public static void main(String[] args) {
            // Test
            System.out.println(Arrays.toString(solution("asdfg")));
            System.out.println(Arrays.toString(solution("asdfgghk")));
        }
    
        public static String[] solution(String s) {
            // Split the string on every 2 characters
            String[] arr = s.split("(?<=\\G..)");
    
            // If the last element of the array is short of one character, append '_' to it
            if (arr.length > 1 && arr[arr.length - 1].length() != 2) {
                arr[arr.length - 1] += "_";
            }
            return arr;
        }
    }
    

    Output:

    [as, df, g_]
    [as, df, gg, hk]
    

    Note: The regex, (?<=\\G..) has been explained below:

    1. \G asserts position at the end of the previous match or the start of the string for the first match
    2. . matches any character (except for line terminators)