java , inserting between duplicate chars in String [closed]

Well, you could try:

Example 1: Using REGEX

public static void main(String[] args) {
        String text = "Hello worlld this is someething cool!";
        //add # between all double letters
        String processingOfText = text.replaceAll("(\\w)\\1", "$1#$1");
        System.out.println(processingOfText);

    }

Example 2: Using string manipulations

public static void main(String[] args) {
        String text = "Hello worlld this is someething cool!";
        for (int i = 1; i < text.length(); i++) 
        {
            if (text.charAt(i) == text.charAt(i - 1)) 
            {
                text = text.substring(0, i) + "#" + text.substring(i, text.length());
            }
        }
        System.out.println(text);

    }

And a lot more…

Browse More Popular Posts

Leave a Comment