🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #8446

Re: StringReplace

#8446
From: Torsten Welches
Date: Thu, 22 Mar 2001 06:57
56 lines
3195 bytes
Tim,

here's what I use:

Torsten

/**
   * Returns a new string resulting from replacing all occurrences of
   * <code>oldSubstring</code> in <code>source</code> with <code>newSubstring</code>.
   * <p>
   * If <code>source</code> is <code>null</code>, <code>null</code> is returned.
   * If <code>oldSubstring</code> does not occur in <code>source</code>,
   * then a new reference to <code>source</code> is returned.
   * Otherwise, a new <code>String</code> object is created that
   * represents a character sequence identical to the character sequence
   * represented by <code>source</code>, except that every
   * occurrence of <code>oldSubstring</code> is replaced by an occurrence
   * of <code>newSubstring</code>.
   * <p>
   *
   * @param   source   the original String.
   * @param   oldSubstring   the old substring.
   * @param   newSubstring   the new substring.
   * @return  a string derived from <code>source</code> by replacing every
   *          occurrence of <code>oldSubstring</code> with <code>newSubstring</code>.
   * @exception  IllegalArgumentException  if <code>oldSubstring</code>
   *          is <code>null</code> or <code>length()</code> == 0.
   */
   public static String replace(String source, String oldSubstring, String newSubstring) {
    if (source == null) return null;
    if (oldSubstring == null || oldSubstring.length() == 0) {
      throw new IllegalArgumentException("null or empty oldSubstring not allowed");
    }
    StringBuffer result = new StringBuffer(source.length());
    int start = 0, stop = 0;
    do {
      stop = source.indexOf(oldSubstring,start);
      if (stop < 0) {
        result.append(source.substring(start, source.length()));
        break;
      }
      result.append(source.substring(start, stop));
      result.append(newSubstring != null ? newSubstring : "");
      start = stop + oldSubstring.length();
    } while (start >= 0 && start < source.length());
    return result.toString();
  }

"T. Horto" <horto@home.com> wrote:
>Hello;
>
>    Coming from the Delphi world I get real used to this
>function(StringReplace).  Is there one in Java?  So far I've only found
>String.replace, but that only does chars.  Do, I just have to break down and
>write one?
Technical Details
Message-ID: <3ab9a255$1_2@dnews>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!128.230.129.106!news.maxwell.syr.edu!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!207.105.83.65
References: <3ab99f55$1_2@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,b2d8cd652b51d03,start
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-03-23 04:03:05 PST
From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Sender: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Reply-To: "Torsten Welches" <torsten.welches@who.cares.netcologne.de>
Subject: Re: StringReplace
Newsgroups: borland.public.jbuilder.java.language
X-User-Info: 193.158.55.74 193.158.55.74    
References:  <3ab99f55$1_2@dnews> 
NNTP-Posting-Host: 207.105.83.65
Message-ID: <3ab9a255$1_2@dnews>
Date: 21 Mar 2001 22:57:25 -0800
X-Trace: 21 Mar 2001 22:57:25 -0800, 207.105.83.65
Lines: 58
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!128.230.129.106!news.maxwell.syr.edu!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!207.105.83.65