Posted By

mscribellito on 03/28/12


Tagged

php java String


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Hero


PHP String


 / Published in: PHP
 

A PHP Class similar to Java's String Object

  1. <?php
  2. if (function_exists("String") == false) {
  3. function String($value = "")
  4. {
  5. return new String($value);
  6. }
  7. }
  8. final class String
  9. {
  10. private $value;
  11. private $count;
  12. public function __construct($value = "")
  13. {
  14. $this->value = (string) $value;
  15. $this->count = strlen($this->value);
  16. }
  17. public function __destruct() {
  18. $this->value = "";
  19. $this->count = 0;
  20. }
  21. public function charAt($index)
  22. {
  23. if ($index < 0 || $index >= $this->count) {
  24. throw new Exception("String index out of range: " . $index);
  25. }
  26. return $this->value[$index];
  27. }
  28. public function codePointAt($index)
  29. {
  30. return ord($this->charAt($index));
  31. }
  32. public function compareTo($str)
  33. {
  34. return strcmp($this->value, $str);
  35. }
  36. public function compareToIgnoreCase($str)
  37. {
  38. return strcasecmp($this->value, $str);
  39. }
  40. public function concat($str)
  41. {
  42. return new String($this->value . $str);
  43. }
  44. public function contains($str)
  45. {
  46. return $this->indexOf($str) > -1;
  47. }
  48. public function endsWith($str)
  49. {
  50. return preg_match("/" . preg_quote($suffix) . "$/", $this->value);
  51. }
  52. public function equals($str)
  53. {
  54. return $this->compareTo($str) == 0;
  55. }
  56. public function equalsIgnoreCase($str)
  57. {
  58. return $this->compareToIgnoreCase($str) == 0;
  59. }
  60. public static function format()
  61. {
  62. $args = func_get_args();
  63. if (count($args) > 0) {
  64. return new String(call_user_func_array("sprintf", $args));
  65. }
  66. }
  67. public function hashCode()
  68. {
  69. $h = 0;
  70. for ($i = 0, $l = $this->count; $i < $l; $i++) {
  71. $h = 31 * $h + $this->codePointAt($i);
  72. }
  73. return $h;
  74. }
  75. public function indexOf($str, $fromIndex = null)
  76. {
  77. if ($fromIndex == null) {
  78. $index = strpos($this->value, $str);
  79. } else {
  80. if ($fromIndex < 0 || $fromIndex >= $this->count) {
  81. throw new Exception("String index out of range: " . $fromIndex);
  82. }
  83. $index = strpos($this->value, $str, $fromIndex);
  84. }
  85. return ($index != false) ? $index : -1;
  86. }
  87. public function isEmpty()
  88. {
  89. return $this->count == 0;
  90. }
  91. public function lastIndexOf($str, $fromIndex = null)
  92. {
  93. if ($fromIndex == null) {
  94. $index = strrpos($this->value, $str);
  95. } else {
  96. if ($fromIndex < 0 || $fromIndex >= $this->count) {
  97. throw new Exception("String index out of range: " . $fromIndex);
  98. }
  99. $index = strrpos($this->value, $str, $fromIndex);
  100. }
  101. return ($index != false) ? $index : -1;
  102. }
  103. public function length()
  104. {
  105. return $this->count;
  106. }
  107. public function matches($regex)
  108. {
  109. return preg_match($regex, $this->value) == 1;
  110. }
  111. public function regionMatches($offsetA, $str, $offsetB, $length)
  112. {
  113. if ($str instanceof String == false) {
  114. $str = new String($str);
  115. }
  116. return $this->substring($offsetA, $length)->equals($str->substring($offsetB, $length));
  117. }
  118. public function regionMatchesIgnoreCase($offsetA, $str, $offsetB, $length)
  119. {
  120. if ($str instanceof String == false) {
  121. $str = new String($str);
  122. }
  123. return $this->substring($offsetA, $length)->equalsIgnoreCase($str->substring($offsetB, $length));
  124. }
  125. public function replace($old, $new)
  126. {
  127. return new String(str_replace($old, $new, $this->value));
  128. }
  129. public function replaceAll($regex, $str, $limit = null)
  130. {
  131. if ($limit == null) {
  132. return new String(preg_replace($regex, $str, $this->value));
  133. }
  134. return new String(preg_replace($regex, $str, $this->value, $limit));
  135. }
  136. public function replaceFirst($regex, $str)
  137. {
  138. return $this->replaceAll($regex, $str, 1);
  139. }
  140. public function reverse()
  141. {
  142. return new String(strrev($this->value));
  143. }
  144. public function split($regex, $limit = null)
  145. {
  146. return preg_split($regex, $this->value, $limit);
  147. }
  148. public function startsWith($prefix)
  149. {
  150. return preg_match("/^" . preg_quote($prefix) . "/", $this->value);
  151. }
  152. public function substring($beginIndex, $endIndex = null)
  153. {
  154. if ($endIndex == null) {
  155. $endIndex = $this->count - 1;
  156. }
  157. if ($beginIndex < 0) {
  158. throw new Exception("String index out of range: " . $beginIndex);
  159. }
  160. if ($endIndex >= $this->count) {
  161. throw new Exception("String index out of range: " . $endIndex);
  162. }
  163. if ($beginIndex > $endIndex) {
  164. throw new Exception("String index out of range: " . $endIndex - $beginIndex);
  165. }
  166. return new String(substr($this->value, $beginIndex, $endIndex - $beginIndex + 1));
  167. }
  168. public function toLowerCase()
  169. {
  170. return new String(strtolower($this->value));
  171. }
  172. public function toUpperCase()
  173. {
  174. return new String(strtoupper($this->value));
  175. }
  176. public function trim()
  177. {
  178. return new String(trim($this->value));
  179. }
  180. public function __toString()
  181. {
  182. return $this->value;
  183. }
  184. }
  185. // EOF: String.php

Report this snippet  

You need to login to post a comment.