check And Remove Duplicates in String - Java java.lang

Java examples for java.lang:String Repeat

Description

check And Remove Duplicates in String

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "java2s.com";
        System.out.println(checkAndRemoveDuplicates(str));
    }//from  w  w w.jav  a 2  s  .  c  o  m

    public static boolean checkAndRemoveDuplicates(final String str) {

        final StringBuilder strbuild = new StringBuilder();
        int flag = 0;

        for (int i = 0; i < str.length(); i++) {
            final String tempStr = str.substring(i, i + 1);

            if (str.charAt(i) != ' ')
                if (strbuild.indexOf(tempStr) == -1) {
                    strbuild.append(tempStr);
                } else {
                    flag = 1;
                    continue;
                }
        }

        System.out.println("After removing duplicates :"
                + strbuild.toString());

        if (flag == 1)
            return true;

        return false;
    }
}

Related Tutorials