Java - Write code to remove Adjacent Underscores

Requirements

Write code to remove Adjacent Underscores

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String string = "boo__k_2s.com";
        System.out.println(removeAdjacentUnderscores(string));
    }//from   www .j  ava2s.c o m

    public static String removeAdjacentUnderscores(String string) {
        StringBuilder builder = new StringBuilder(string);
        for (int i = 0; i < builder.length() - 1; i++) {
            if (builder.charAt(i) == '_' && builder.charAt(i + 1) == '_') {
                builder.deleteCharAt(i);
                i--;
            }
        }
        return builder.toString();
    }
}

Related Exercise