<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Kommentare zu: Javascript printf</title>
	<atom:link href="http://www.naden.de/blog/javascript-printf/feed" rel="self" type="application/rss+xml" />
	<link>http://www.naden.de/blog/javascript-printf</link>
	<description>/* no comment */</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:31:26 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
	<item>
		<title>Von: BananaAcid</title>
		<link>http://www.naden.de/blog/javascript-printf/comment-page-1#comment-53240</link>
		<dc:creator>BananaAcid</dc:creator>
		<pubDate>Sat, 27 Mar 2010 16:35:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.naden.de/blog/javascript-printf#comment-53240</guid>
		<description>Slight modification to make it even more usefull... :-D should be comined with the array version. will see if can consolidate both code blocks.

String.prototype.sprintf = function() {
 if( arguments.length &lt; 1 ) {
  return this;
 }
 
 var data = this; //arguments[ 0 ];
 
 for( var k=0; k&lt;arguments.length; ++k ) {
 
  switch( typeof( arguments[ k ] ) )
  {
   case &#039;string&#039;:
    data = data.replace( /%s/, arguments[ k ] );
    break;
   case &#039;number&#039;:
    data = data.replace( /%d/, arguments[ k ] );
    break;
   case &#039;boolean&#039;:
    data = data.replace( /%b/, arguments[ k ] ? &#039;true&#039; : &#039;false&#039; );
    break;
   default:
    /// function &#124; object &#124; undefined
    break;
  }
 }
 return( data );
}


var x= &quot;My cat %d likes fish!&quot;.sprintf(&quot;Joy&quot;);

document.write( x );</description>
		<content:encoded><![CDATA[<p>Slight modification to make it even more usefull&#8230; :-D should be comined with the array version. will see if can consolidate both code blocks.</p>
<p>String.prototype.sprintf = function() {<br />
 if( arguments.length &lt; 1 ) {<br />
  return this;<br />
 }</p>
<p> var data = this; //arguments[ 0 ];</p>
<p> for( var k=0; k&lt;arguments.length; ++k ) {</p>
<p>  switch( typeof( arguments[ k ] ) )<br />
  {<br />
   case &#039;string&#039;:<br />
    data = data.replace( /%s/, arguments[ k ] );<br />
    break;<br />
   case &#039;number&#039;:<br />
    data = data.replace( /%d/, arguments[ k ] );<br />
    break;<br />
   case &#039;boolean&#039;:<br />
    data = data.replace( /%b/, arguments[ k ] ? &#039;true&#039; : &#039;false&#039; );<br />
    break;<br />
   default:<br />
    /// function | object | undefined<br />
    break;<br />
  }<br />
 }<br />
 return( data );<br />
}</p>
<p>var x= &quot;My cat %d likes fish!&quot;.sprintf(&quot;Joy&quot;);</p>
<p>document.write( x );</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: naden</title>
		<link>http://www.naden.de/blog/javascript-printf/comment-page-1#comment-5964</link>
		<dc:creator>naden</dc:creator>
		<pubDate>Fri, 07 Dec 2007 15:18:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.naden.de/blog/javascript-printf#comment-5964</guid>
		<description>Ist nun online. F&#228;lle in denen als Argumente Arrays und andere Werte gemixt vorkommen werden auch hier noch nicht gehandelt. Eine Verbesserung ist es allemal.
Ein Aufruf wie der Folgende ist nun m&#246;glich:

alert( sprintf( &#039;%d - %d = %d&#039;, [ 2, 3, -1 ] ) );

Nat&#252;rlich kann das Array auch eine Variable sein:

var list = [ 2, 3, -1 ];

alert( sprintf( &#039;%d - %d = %d&#039;, list ) );</description>
		<content:encoded><![CDATA[<p>Ist nun online. F&#228;lle in denen als Argumente Arrays und andere Werte gemixt vorkommen werden auch hier noch nicht gehandelt. Eine Verbesserung ist es allemal.<br />
Ein Aufruf wie der Folgende ist nun m&#246;glich:</p>
<p>alert( sprintf( &#8216;%d &#8211; %d = %d&#8217;, [ 2, 3, -1 ] ) );</p>
<p>Nat&#252;rlich kann das Array auch eine Variable sein:</p>
<p>var list = [ 2, 3, -1 ];</p>
<p>alert( sprintf( &#8216;%d &#8211; %d = %d&#8217;, list ) );</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: naden</title>
		<link>http://www.naden.de/blog/javascript-printf/comment-page-1#comment-5937</link>
		<dc:creator>naden</dc:creator>
		<pubDate>Thu, 06 Dec 2007 10:52:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.naden.de/blog/javascript-printf#comment-5937</guid>
		<description>Ich hatte dir dazu eine E-Mail geschrieben. Es scheint so als ob der Content-Filter von Wordpress den Javascript-Code verschluckt. Wegen XSS etc. Wenn du mir einfach auf die Mail antwortest, dann editier ich das im Backend.</description>
		<content:encoded><![CDATA[<p>Ich hatte dir dazu eine E-Mail geschrieben. Es scheint so als ob der Content-Filter von WordPress den Javascript-Code verschluckt. Wegen XSS etc. Wenn du mir einfach auf die Mail antwortest, dann editier ich das im Backend.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Baby Herman</title>
		<link>http://www.naden.de/blog/javascript-printf/comment-page-1#comment-5936</link>
		<dc:creator>Baby Herman</dc:creator>
		<pubDate>Thu, 06 Dec 2007 10:41:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.naden.de/blog/javascript-printf#comment-5936</guid>
		<description>Gibt es eine M&#246;glichkeit, mein Kommentar komplett darzustellen?</description>
		<content:encoded><![CDATA[<p>Gibt es eine M&#246;glichkeit, mein Kommentar komplett darzustellen?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Von: Baby Herman</title>
		<link>http://www.naden.de/blog/javascript-printf/comment-page-1#comment-5933</link>
		<dc:creator>Baby Herman</dc:creator>
		<pubDate>Thu, 06 Dec 2007 09:34:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.naden.de/blog/javascript-printf#comment-5933</guid>
		<description>Erst einmal gro&#223;es Lob f&#252;r die so sch&#246;n einfache L&#246;sung einer sprintf-Funktion.

Auch in PHP hat diese Funktion jedoch den Nachteil: Sie kann nicht mit Arrays umgehen. Als L&#246;sung wird hier die vsprintf-Funktion geboten, die zus&#228;tzlich den Umgang mit Arrays bereit h&#228;lt.

Hier eine kleine Erweiterung deiner Funktion f&#252;r den Umgang mit Arrays:

function sprintf() {
  if( sprintf.arguments.length &lt; 2 ) return;

  var data = sprintf.arguments[ 0 ];

  if( typeof( sprintf.arguments[ 1 ] ) == &#039;object&#039; ) {
   var k = 0;
   var replace = sprintf.arguments[ 1 ];
  }
  else {
   var k = 1;
   var replace = sprintf.arguments;
  }
                
  for( k; k&lt;replace.length; ++k ) {
   switch( typeof( replace[ k ] ) ) {
    case &#039;string&#039;:
     data = data.replace( /%s/, replace[ k ] );
     break;
    case &#039;number&#039;:
     data = data.replace( /%d/, replace[ k ] );
     break;
    case &#039;boolean&#039;:
     data = data.replace( /%b/, replace[ k ] ? &#039;true&#039; : &#039;false&#039; );
     break;
    default:
     /// function &#124; object &#124; undefined
     break;
   }
  }

  return( data );
}</description>
		<content:encoded><![CDATA[<p>Erst einmal gro&#223;es Lob f&#252;r die so sch&#246;n einfache L&#246;sung einer sprintf-Funktion.</p>
<p>Auch in PHP hat diese Funktion jedoch den Nachteil: Sie kann nicht mit Arrays umgehen. Als L&#246;sung wird hier die vsprintf-Funktion geboten, die zus&#228;tzlich den Umgang mit Arrays bereit h&#228;lt.</p>
<p>Hier eine kleine Erweiterung deiner Funktion f&#252;r den Umgang mit Arrays:</p>
<p>function sprintf() {<br />
  if( sprintf.arguments.length < 2 ) return;</p>
<p>  var data = sprintf.arguments[ 0 ];</p>
<p>  if( typeof( sprintf.arguments[ 1 ] ) == &#8216;object&#8217; ) {<br />
   var k = 0;<br />
   var replace = sprintf.arguments[ 1 ];<br />
  }<br />
  else {<br />
   var k = 1;<br />
   var replace = sprintf.arguments;<br />
  }</p>
<p>  for( k; k<replace.length; ++k ) {<br />
   switch( typeof( replace[ k ] ) ) {<br />
    case &#8216;string&#8217;:<br />
     data = data.replace( /%s/, replace[ k ] );<br />
     break;<br />
    case &#8216;number&#8217;:<br />
     data = data.replace( /%d/, replace[ k ] );<br />
     break;<br />
    case &#8216;boolean&#8217;:<br />
     data = data.replace( /%b/, replace[ k ] ? &#8216;true&#8217; : &#8216;false&#8217; );<br />
     break;<br />
    default:<br />
     /// function | object | undefined<br />
     break;<br />
   }<br />
  }</p>
<p>  return( data );<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>

