balafi
balafi

Reputation: 2153

Print percent character

console.log("%test"); // %test
console.log("%%test"); // %test
console.log("%%%test"); // %%test
etc

Say I have a string that may or may not have a number of consecutive '%' How can I print it correctly (with all percent characters)?

Example:

var myString = getStringFromAnAPi();  // say, the API returns "ping%%%pong"
console.log(myString)   // ping%%pong   -> missing one %

Upvotes: 1

Views: 2972

Answers (2)

A.T.
A.T.

Reputation: 26312

this way you can append text to %,

console.log("%",'test'); // %test
console.log("%%","test"); // %test
console.log("%%%","test"); // %%test

in html,

<div>&#37;test</div>
<div>&#37;&#37;test</div>
<div>&#37;&#37;&#37;test</div>

output

%test
%%test
%%%test

Upvotes: 0

GuyT
GuyT

Reputation: 4416

You can do that by: console.log('%s%%%', 'test');. See JSFiddle: http://jsfiddle.net/8B5Vh/

Upvotes: 2

Related Questions