public static int relevance(String value, String search) {

		if (StringUtils.isNullOrEmptyTrimmed(value) ||
			StringUtils.isNullOrEmptyTrimmed(search)) {
			return -1;
		}

		if (search.length() > value.length()) {
			return -1;
		}

		int relevance = -1; // start at -1 ... so -1 is returned for no result
		int delta = 1; // first delta at 1 ... producing a sum of 0 when first letter is found

		int len = value.length();
		int searchLen = search.length();
		int letterIndex = 0;

		for (int searchIndex = 0; searchIndex < searchLen; searchIndex++) {

			char match = search.charAt(searchIndex);

			while (letterIndex < len) {
				char letter = value.charAt(letterIndex);
				letterIndex++;

				if (match == letter) {
					relevance = relevance + delta; // reverse to get higher value for better match
					delta = 0;
					break;
				}
				else {
					delta++;
				}
			}

			// we matched all characters ... and found the last one ...
			if (delta == 0 && searchIndex == searchLen - 1) {
				return relevance;
			}

			if (letterIndex == len) {
				break;
			}
		}

		return -1;
	}
public static int relevance(String value, String search) {

		if (StringUtils.isNullOrEmptyTrimmed(value) ||
			StringUtils.isNullOrEmptyTrimmed(search)) {
			return -1;
		}

		if (search.length() > value.length()) {
			return -1;
		}

		int relevance = -1; 
		int delta = 1; 

		int len = value.length();
		int searchLen = search.length();
		int letterIndex = 0;

		for (int searchIndex = 0; searchIndex < searchLen; searchIndex++) {

			char match = search.charAt(searchIndex);

			while (letterIndex < len) {
				char letter = value.charAt(letterIndex);
				letterIndex++;

				if (match == letter) {
					relevance = relevance + delta; 
					delta = 0;
					break;
				}
				else {
					delta++;
				}
			}

			
			if (delta == 0 && searchIndex == searchLen - 1) {
				return relevance;
			}

			if (letterIndex == len) {
				break;
			}
		}

		return -1;
	}
public static int relevance ( String value , String search ) { if ( StringUtils . isNullOrEmptyTrimmed ( value ) || StringUtils . isNullOrEmptyTrimmed ( search ) ) { return - 1 ; } if ( search . length ( ) > value . length ( ) ) { return - 1 ; } int relevance = - 1 ; // start at -1 ... so -1 is returned for no result int delta = 1 ; // first delta at 1 ... producing a sum of 0 when first letter is found int len = value . length ( ) ; int searchLen = search . length ( ) ; int letterIndex = 0 ; for ( int searchIndex = 0 ; searchIndex < searchLen ; searchIndex ++ ) { char match = search . charAt ( searchIndex ) ; while ( letterIndex < len ) { char letter = value . charAt ( letterIndex ) ; letterIndex ++ ; if ( match == letter ) { relevance = relevance + delta ; // reverse to get higher value for better match delta = 0 ; break ; } else { delta ++ ; } } // we matched all characters ... and found the last one ... if ( delta == 0 && searchIndex == searchLen - 1 ) { return relevance ; } if ( letterIndex == len ) { break ; } } return - 1 ; }
Calculates matching relevance between given string and search expression

@param value  to search in
@param search to search for
@return -1 not relevant, 0..N - where lower values represents more relevant results
Calculates matching relevance between given string and search expression