0

In Python there is a string fromatter like;

>>> print 'Name :%s Surname :%s'%('Ahmet','DAL')
Name :Ahmet Surname :DAL

Are there any native string formating method in Javascript, which has simple usage as Python has?

Edited: There are methods which is using external library like "sprintf", etc. But I'm just curious, Is there a native method in Javascript?

Thank You!

Ahmet DAL
  • 4,445
  • 9
  • 47
  • 71
  • I've answered this before: http://stackoverflow.com/questions/13639464/javascript-equivalent-to-pythons-format – Steven Moseley Apr 22 '13 at 11:06
  • Your anwser does not provide a native method usage. You are adding in String prototype yourself. Please read the question exactly. I'm just curious about whether Javascript has a "NATIVE" method for this or not. – Ahmet DAL Apr 22 '13 at 11:14
  • Check out this library https://github.com/zsong/Kiwi - it does what you want but is a lib. – Henrik Andersson Apr 22 '13 at 11:26

2 Answers2

1

No, there is no a native string formatting method in Javascript like str.format in Python.

warvariuc
  • 57,116
  • 41
  • 173
  • 227
0

You can use Javascript's sprintf.

The sprintf equivalent example you provided will be:

sprintf('Name: %1$s Surname :%2$s', 'Ahmet','DAL');

You can also change the arguments' order:

sprintf('Name: %2$s Surname :%1$s', 'DAL', 'Ahmet');

It is open source and you can download it here.

Thanasis Petsas
  • 4,378
  • 5
  • 31
  • 57
  • Actually, I'm asking for any native method in Javascript like in Python. "sprintf" is not native method in Javascript. – Ahmet DAL Apr 22 '13 at 11:10