Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  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. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement