Advertisement
dim4o

UsefulStringMethods

Mar 12th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Returns true if the string starts with the provided substring and false otherwise
  3. String.prototype.startsWith = function startsWith(substring) {
  4.  
  5.     return (this.indexOf(substring) == 0);
  6. };
  7.  
  8. // Returns true if the string ends with the provided substring and false otherwise
  9. String.prototype.endsWith = function endsWith(substring) {
  10.  
  11.     var len = substring.length;
  12.     return (this.indexOf(substring) == this.length - len);
  13. };
  14.  
  15. // Returns the first count characters of the string. If count is greater than the
  16. // length of the string, returns the whole string
  17. String.prototype.left = function left(count){
  18.  
  19.     if (count > this.left) {
  20.         count = this.length;
  21.     }
  22.     return this.substr(0, count);
  23. };
  24.  
  25. // Returns the last count characters of the string. If count is greater than the
  26. // length of the string, returns the whole string
  27. String.prototype.right = function right(count){
  28.  
  29.     if (count > this.left) {
  30.         count = this.length;
  31.     }
  32.  
  33.     return this.substr(this.length - count  , this.length - 1);
  34. };
  35.  
  36. // Returns a new string which contains count times the specified character at its beginning.
  37. // Character is optional and defaults to space
  38. String.prototype.padLeft = function padLeft(count, character) {
  39.     if (count < this.length) {
  40.         return this.toString();
  41.     }
  42.     if (arguments.length == 1) {
  43.         character = " ";
  44.     }
  45.     var help = character.repeat(count);
  46.     return (help + this).right(count);
  47. };
  48.  
  49. // returns a new string which contains count times the specified character at its end.
  50. // character is optional and defaults to space
  51. String.prototype.padRight = function padRight(count, character) {
  52.     if (count < this.length) {
  53.         return this.toString();
  54.     }
  55.     if (arguments.length == 1) {
  56.         character = " ";
  57.     }
  58.     var help = character.repeat(count);
  59.     return (this + help).left(count);
  60. };
  61.  
  62. // Repeats the provided string count times. Do not use
  63. // the default implementation here, write your own
  64. String.prototype.repeat = function repeat(count) {
  65.  
  66.     return new Array(count + 1).join(this);
  67. };
  68.  
  69. // TESTS
  70. var example1 = "TThis is an example string used only for demonstration purposes.";
  71. console.log(example1.startsWith("This"));
  72. console.log(example1.startsWith("this"));
  73. console.log(example1.startsWith("other"));
  74. console.log();
  75.  
  76. var example2 = "This is an example string used only for demonstration purposes.";
  77. console.log(example2.endsWith("poses."));
  78. console.log(example2.endsWith ("example"));
  79. console.log(example2.startsWith("something else"));
  80. console.log();
  81.  
  82. var example3 = "This is an example string used only for demonstration purposes.";
  83. console.log(example3.left(9));
  84. console.log(example3.left(90));
  85. console.log();
  86.  
  87. var example4 = "This is an example string used only for demonstration purposes.";
  88. console.log(example4.right(9));
  89. console.log(example4.right(90));
  90. console.log();
  91.  
  92. var example4_1 = "abcdefgh";
  93. console.log(example4_1.left(5).right(2));
  94. console.log();
  95.  
  96. var hello1 = "hello";
  97. console.log(hello1.padLeft(5));
  98. console.log(hello1.padLeft(10));
  99. console.log(hello1.padLeft(5, "."));
  100. console.log(hello1.padLeft(10, "."));
  101. console.log(hello1.padLeft(2, "."));
  102. console.log();
  103.  
  104. var hello2 = "hello";
  105. console.log(hello2.padRight(5));
  106. console.log(hello2.padRight(10));
  107. console.log(hello2.padRight(5, "."));
  108. console.log(hello2.padRight(10, "."));
  109. console.log(hello2.padRight(2, "."));
  110.  
  111. var character = "*";
  112. console.log(character.repeat(5));
  113. // Alternative syntax
  114. console.log("~".repeat(3));
  115. console.log();
  116.  
  117. // Another combination
  118. console.log("*".repeat(5).padLeft(10, "-").padRight(15, "+"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement