SHARE
TWEET

Untitled

a guest Mar 25th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. PHP example:
  2. ```
  3. $location = 'tree';
  4. $format = 'There are 5 monkeys in the %s';
  5. echo sprintf($format, $location);
  6. #output: There are 5 monkeys in the tree
  7. ```
  8. Java String.format
  9.  
  10. ```
  11. String monkeyHome = "tree";
  12.  
  13. //System output
  14. System.out.println(String.format("There are 5 monkeys in the %s", monkeyHome));
  15.  
  16. //new variable
  17. String monkeySong = String.format("There are 5 monkeys in the %s", monkeyHome);
  18. You can also use it with multiple variables to replace. This is very usefull for replacing text in text templates:
  19.  
  20. String monkeyHome = "tree";
  21. int monkeyFamSize = 5;
  22.  
  23. //System output
  24. System.out.println(String.format("There are %d monkeys in the %s", monkeyFamSize, monkeyHome));
  25.  
  26. //new variable
  27. String monkeySong = String.format("There are %d monkeys in the %s", monkeyFamSize, monkeyHome);
  28. ```
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top