Article View: borland.public.jbuilder.java.language
Article #7816Re: Format a String
From: D. Michael Nelson
Date: Wed, 16 May 2001 06:46
Date: Wed, 16 May 2001 06:46
39 lines
2116 bytes
2116 bytes
Adrian,
Since strings are immutable, I used a StringBuffer object which allows
modification to the string. Below I listed my version of the sprintf
written java (which is not elagant or optimal). It appears to me that java
is not very flexible in terms maniuplating strings types. Also here is a
link to the java API for class StringBuffer.
http://java.sun.com/j2se/1.3/docs/api/java/lang/StringBuffer.html
Take It Easy,
D. Michael Nelson
public class StringInsert{
public static void main(String[] argc) {
String sGreeting = "Hello %s how are you?";
String sName = "Adrian";
sGreeting = sprintf(sGreeting, sName);
System.out.println(sGreeting);
}
static public String sprintf(String sDest, String sIns) {
StringBuffer sb = new StringBuffer(sDest);
int i=0;
sb.replace(i = sDest.indexOf("%s"), i+2, sIns);
return(sDest = new String(sb));
}
}
Adrian <nospam@anytime.com> wrote in message
news:3b020032.21421141@forums.borland.com...
> Is there a specific Class that can be used to place values into a
> String.
>
> ie
> String s = "Hello %s how are you ?";
> String ns = FormatString (s, "Adrian"); // substitute %s for Adrian
>
Technical Details
Message-ID:
<8jpM6.2083$M8.112842@news1.atl>Path:
arc1-ca.pugleaf.net!archiver1.sj.google.com!newsfeed.google.com!sn-xit-03!supernews.com!zeus.visi.com!news-out.visi.com!hermes.visi.com!feed2.news.rcn.net!rcn!newsfeed.atl!news1.atl.POSTED!not-for-mailReferences:
<3b020032.21421141@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,5539798795b74f52 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-05-15 23:44:02 PST Path: arc1-ca.pugleaf.net!archiver1.sj.google.com!newsfeed.google.com!sn-xit-03!supernews.com!zeus.visi.com!news-out.visi.com!hermes.visi.com!feed2.news.rcn.net!rcn!newsfeed.atl!news1.atl.POSTED!not-for-mail From: "D. Michael Nelson" <dmn1@bellsouth.net> Newsgroups: borland.public.jbuilder.java.language References: <3b020032.21421141@forums.borland.com> Subject: Re: Format a String Lines: 43 X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Message-ID: <8jpM6.2083$M8.112842@news1.atl> Date: Wed, 16 May 2001 02:46:54 -0400 NNTP-Posting-Host: 65.80.84.183 X-Trace: news1.atl 989995332 65.80.84.183 (Wed, 16 May 2001 02:42:12 EDT) NNTP-Posting-Date: Wed, 16 May 2001 02:42:12 EDT