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.