Here you can find the source of countSubstring(final String text, final String substring)
Parameter | Description |
---|---|
text | Texto en el que realizar la búsqueda. |
substring | Subcadena que deseamos buscar. |
public static int countSubstring(final String text, final String substring)
//package com.java2s; /* Copyright (C) 2011 [Gobierno de Espana] * This file is part of "Cliente @Firma". * "Cliente @Firma" is free software; you can redistribute it and/or modify it under the terms of: * - the GNU General Public License as published by the Free Software Foundation; * either version 2 of the License, or (at your option) any later version. * - or The European Software License; either version 1.1 or (at your option) any later version. * Date: 11/01/11// ww w . j a v a 2 s .c o m * You may contact the copyright holder at: soporte.afirma5@mpt.es */ public class Main { /** Cuenta las repeticiones de una subcadena dentro de una cadena. Las * subcadenas no pueden estar acopladas. * @param text Texto en el que realizar la búsqueda. * @param substring Subcadena que deseamos buscar. * @return Número de coincidencias. */ public static int countSubstring(final String text, final String substring) { int count = 0; for (int i = 0; i <= text.length() - substring.length(); i++) { if (substring.charAt(0) == text.charAt(i) && substring.equals(text.substring(i, i + substring.length()))) { count++; i += substring.length() - 1; } } return count; } }