Remove repeated characters

Is there a method in standard Java API that removes the repeated charcter from a string; Eg.If, String str = "baba";then the method should return "ba".
[186 byte] By [sosododoa] at [2007-11-26 23:01:36]
# 1
People accuse Java of being bloated, but it doesn't have things like that. Such a method would only be useful for beginners' homework assignments.
DrClapa at 2007-7-10 13:26:26 > top of Java-index,Java Essentials,New To Java...
# 2

The java.util.regex package contains Java's regular expression library. In particular, you could use the Pattern class to try and find repeating sequences. The Pattern class is described here:

http://java.sun.com/javase/6/docs/api/index.html?java/util/regex/Pattern.html

Given a string like "abab", the code below will produce "ab", but given a string like "ababc" it will still produce "ab" (which might be a problem, depending on what you are trying to do):

String str = ...;

for(int i = 2; i < str.length() / 2; ++i) {

Pattern p = Pattern.compile( str.substring(0, i) );

Matcher m = p.matcher(str);

int matchCount = 0;

while(m.find()) {

++matchCount;

}

if( matchCount > 1 ) {

m.reset();

m.find();

str = m.group();

break;

}

}

If you know that the repetition is at most going to occur twice, you can simply split the string in the middle and compare the first and second halves. If they are equal you can just use the first one, and, if they are not equal, you can use the original String. For example:

//string must have even number of characters

//or there cannot be a symmetric repetition

if( str.length() % 2 == 0 )

{

String firstHalf = str.substring(0, str.length() / 2);

String secondHalf = str.substring( string.length() / 2, str.length() );

//must use equals() method;

//the == operator only compares if the references point to the same location

if( firstHalf.equals(secondHalf) ) {

//there is a repeated sequence; modify str

str = firstHalf;

}

else {

//do nothing; there is no repeated sequence

}

}

Edit:

Sorry, I made a mistake about what groupCount() does. I thought it returned the total number of matches, but it actually returns the number of capturing groups used in the pattern. Both of the algorithms above should work now.

@modia at 2007-7-10 13:26:26 > top of Java-index,Java Essentials,New To Java...
# 3
You mean something likeString line = "babababababababab";line = line.replaceAll("(.)(?=.*?\\1)","");System.out.println(line);
sabre150a at 2007-7-10 13:26:26 > top of Java-index,Java Essentials,New To Java...