PageRenderTime 33ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/scripts/string.js

http://showslow.googlecode.com/
JavaScript | 43 lines | 35 code | 4 blank | 4 comment | 12 complexity | eab924c496db6ca14cf8a8fb495a7a1e MD5 | raw file
  1. /*==================================================
  2. * String Utility Functions and Constants
  3. *==================================================
  4. */
  5. String.prototype.trim = function() {
  6. return this.replace(/^\s+|\s+$/g, '');
  7. };
  8. String.prototype.startsWith = function(prefix) {
  9. return this.length >= prefix.length && this.substr(0, prefix.length) == prefix;
  10. };
  11. String.prototype.endsWith = function(suffix) {
  12. return this.length >= suffix.length && this.substr(this.length - suffix.length) == suffix;
  13. };
  14. String.substitute = function(s, objects) {
  15. var result = "";
  16. var start = 0;
  17. while (start < s.length - 1) {
  18. var percent = s.indexOf("%", start);
  19. if (percent < 0 || percent == s.length - 1) {
  20. break;
  21. } else if (percent > start && s.charAt(percent - 1) == "\\") {
  22. result += s.substring(start, percent - 1) + "%";
  23. start = percent + 1;
  24. } else {
  25. var n = parseInt(s.charAt(percent + 1));
  26. if (isNaN(n) || n >= objects.length) {
  27. result += s.substring(start, percent + 2);
  28. } else {
  29. result += s.substring(start, percent) + objects[n].toString();
  30. }
  31. start = percent + 2;
  32. }
  33. }
  34. if (start < s.length) {
  35. result += s.substring(start);
  36. }
  37. return result;
  38. };