Check Jef Poskanzer's Acme classes:

http://www.acme.com

He include a Utils class with some string stuff. There's also a very
nice Perl (or other) style regexp package at

http://www.cacas.org/java/gnu/regexp/

There are lots of other Java utility packages wandering around on the
web, as well.

As for a general replace method, here's one:

    /**
     * Replace all occurences of a string with another string.
     */
    public static String replace(
        String inputString,
        String findString,
        String replaceWithString
    ) {
        StringBuffer sb = new StringBuffer();
        int index = -1;
        int start = 0;
        int findLength = findString.length();

        // Loop over occurrences of string to replace.
        while ((index = inputString.indexOf(findString, start)) != -1) {
            sb.append(inputString.substring(start, index));
            sb.append(replaceWithString);
            start = index + findLength;
        }

        // Add the rest.
        sb.append(inputString.substring(start));
        return sb.toString();
    }

In general, though, if you find you're using such methods a *lot* you
might want to look into representing things as classes instead of as
structured strings.

Rod McChesney, Korobra Corporation


Oliver Pistor wrote:
>
> Hi!
>
> I'm just fighting with the incomplete String classes.
>
> I urgently need methods like split() [not only a split by a single character
> token, but a split with a string as token] or
> some decent replace() and all the "string-stuff" you need to build dynamic
> database queries, HTML pages etc.
>
> I was wondering if anyone had the same problems and has written some code or
> if there are
> any classes or something like a "Utility Servlet" (maybe we should do one?)
> available
>
> Oliver
>
> -----------------------------------------------------------------------
> Oliver Pistor
> dsa Solutions
> Pf�lzer Str.6
> 69123 Heidelberg
>
> Tel.: 06221 839 513
> Mobil: 0172 761 9365
> Fax: +49 6221 472675
>
> [EMAIL PROTECTED]
> http://www.dsa-solutions.de
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to