returns a string buffer of characters from string1 within string2 if they are of a given distance separation from the position in string1. - Java java.lang

Java examples for java.lang:String Distance

Description

returns a string buffer of characters from string1 within string2 if they are of a given distance separation from the position in string1.

Demo Code

////from   w  w  w . j a  v  a 2 s .c o m
// Copyright (C) 2006-2014 Talend Inc. - www.talend.com
//
// This source code is available under agreement available at
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
//
// You should have received a copy of the agreement
// along with this program; if not, write to Talend SA
// 9 rue Pages 92150 Suresnes, France
//

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String string1 = "java2s.com";
        String string2 = "java2s.com";
        int distanceSep = 42;
        System.out.println(getCommonCharacters(string1, string2,
                distanceSep));
    }

    /**
     * returns a string buffer of characters from string1 within string2 if they are of a given distance separation from
     * the position in string1.
     * 
     * @param string1
     * @param string2
     * @param distanceSep
     * @return a string buffer of characters from string1 within string2 if they are of a given distance separation from
     * the position in string1
     */
    public static StringBuffer getCommonCharacters(final String string1,
            final String string2, final int distanceSep) {
        // create a return buffer of characters
        final StringBuffer returnCommons = new StringBuffer();
        // create a copy of string2 for processing
        final StringBuffer copy = new StringBuffer(string2);
        // iterate over string1
        for (int i = 0; i < string1.length(); i++) {
            final char ch = string1.charAt(i);
            // set boolean for quick loop exit if found
            boolean foundIt = false;
            // compare char with range of characters to either side
            // MOD scorreia 2010-01-25 for identical strings, this method should return the full input string. I checked
            // against second string and it now gives the same results
            for (int j = Math.max(0, i - distanceSep); !foundIt
                    && j < Math.min(i + distanceSep + 1, string2.length()); j++) {
                // for (int j = Math.max(0, i - distanceSep); !foundIt && j < Math.min(i + distanceSep, string2.length()
                // - 1); j++) {
                // check if found
                if (copy.charAt(j) == ch) {
                    foundIt = true;
                    // append character found
                    returnCommons.append(ch);
                    // alter copied string2 for processing
                    copy.setCharAt(j, (char) 0);
                }
            }
        }
        return returnCommons;
    }
}

Related Tutorials