All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.string.SubstringCounter Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.string;

public class SubstringCounter {

	/**
	 * Determines the number of occurrences of the given "needle" {@link String}
	 * in the given "haystack" {@link String}.
	 * 

* If either of the parameters is null or empty, 0 is returned. *

* This method is case sensitive. * * @param haystack * the {@link String} to search for occurrences (may be * null) * @param needle * the occurring {@link String} (may be null) * @return the number of occurrences */ public static int countOccurrences(String haystack, String needle) { if (isEmpty(haystack) || isEmpty(needle)) { return 0; } int lastIndex = 0; int count = 0; while (lastIndex != -1) { lastIndex = haystack.indexOf(needle, lastIndex); if (lastIndex != -1) { count++; lastIndex += needle.length(); } } return count; } private static boolean isEmpty(String string) { return string == null || string.length() == 0; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy