Find two equal substrings in a string I have various strings that contain codes, e.g. like this:

"file = new JarFile(new File()" + "\nSystem.out.println(file"


but also

"args"


Now I want to find the substring that is in the beginning and in the end of the string. E.g. in the first case the result would be file. In the second example the result has to be args

I now wrote the following method:

public static String findWord(String text) {
        String result = null;
        boolean found = false;
        for (int i = 0; i &lt; text.length(); i++) {
            for (int j = 0; j &lt; text.length(); j++) {
                String first = text.substring(0, i*j);
                String last = text.substring(text.length() - i*j, text.length());
                if (first.equals(last) &amp;&amp; first.length() &gt; 0) {
                    result = first;
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }
        return result;
    }


This seems to work for the test cases I tested, but I'm not sure if it works for all cases. Additionally, I'm not sure if there isn't a simpler solution. I need two loops and ugly break statements. So how could I improve that?
I'm not completely sure about the requirements, but what about the following?

public static String findWord2(final String text) {
    for (int i = text.length() - 1; i &gt; 0 ; i--) {
        final String first = text.substring(0, i);
        final String last = text.substring(text.length() - i);
        if (first.equals(last)) {
            return first;
        }
    }
    return text;
}


Here are my tests:

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class FindWords2Test {

    private final String expected;
    private final String input;

    public FindWords2Test(final String expected, final String input) {
        this.expected = expected;
        this.input = input;
    }

    @Parameters
    public static Collection&lt;String[]&gt; data() {
        //@formatter:off
        final String[][] data = new String[][] { 
                {"file", "file = new JarFile(new File()\nSystem.out.println(file"},
                {"file ", "file = new JarFile(new File()\nSystem.out.println(file "},
                {"file = new JarFile(new File()\nSystem.out.println(", 
                    "file = new JarFile(new File()\nSystem.out.println("}, 
                {"args", "args"},
                {"", ""}, 
                {"a", "a"}, 
                {"a", "aa"}};
        //@formatter:on
        return Arrays.asList(data);
    }

    @Test
    public void testFindWords() throws Exception {
        assertEquals(expected, FindWords2.findWord2(input));
    }
}