/* * Copyright (C) 1997 by Pharos IP Pty Ltd * Confidential. All rights reserved. * $Id: StringOps.java,v 1.1 1998-01-08 15:34:40+10 mbp Exp $ * $Source: /fn/cvsroot/pip/java/au/com/pharos/util/StringOps.java,v $ * * ``No strings attached.'' */ package au.com.pharos.util; /** * Mixed bag of string manipulation functions. It would be nice if * some of these were in java.lang.String, but since * they're not, they're implemented as static functions within this * class. * * Typical use: *
 * String foo = "some string"
 * int cnt = StringOps.countOccurrences(foo, 's');
 * // --> cnt == 2
 * 
* * @author Martin Pool * @version $Revision: 1.1 $, $Date: 1998-01-08 15:34:40+10 $ **/ public class StringOps { /** This class contains only static public functions. **/ private StringOps() { ; } /** Count the number of occurences of a character in a * string. * * @param needle the character to search * @param haystack string to be searched * * @return 0 if needle does not occur in haystack; * or a positive number indicating the number of occurrences **/ static public int countOccurrences(String haystack, char needle) { byte[] hay = haystack.getBytes(); int count = 0; for (int i = 0; i < hay.length; i++) if (hay[i] == needle) count++; return count; } }