Java tutorial
public class Main { public static void main(String[] args) { String string = "java 2s.com java"; int length = string.length(); if (length < 2) { System.out.println(string); return; } System.out.print(string.charAt(0)); for (int i = 1; i < length; i++) { if (string.charAt(i) != string.charAt(i - 1)) { System.out.print(string.charAt(i)); } } } }