/ Published in: PHP
A PHP Class similar to Java's String Object
Expand |
Embed | Plain Text
<?php function String($value = "") { return new String($value); } } final class String { private $value; private $count; public function __construct($value = "") { $this->value = (string) $value; } public function __destruct() { $this->value = ""; $this->count = 0; } public function charAt($index) { if ($index < 0 || $index >= $this->count) { throw new Exception("String index out of range: " . $index); } return $this->value[$index]; } public function codePointAt($index) { } public function compareTo($str) { } public function compareToIgnoreCase($str) { } public function concat($str) { return new String($this->value . $str); } public function contains($str) { return $this->indexOf($str) > -1; } public function endsWith($str) { } public function equals($str) { return $this->compareTo($str) == 0; } public function equalsIgnoreCase($str) { return $this->compareToIgnoreCase($str) == 0; } { } } public function hashCode() { $h = 0; for ($i = 0, $l = $this->count; $i < $l; $i++) { $h = 31 * $h + $this->codePointAt($i); } return $h; } public function indexOf($str, $fromIndex = null) { if ($fromIndex == null) { } else { if ($fromIndex < 0 || $fromIndex >= $this->count) { throw new Exception("String index out of range: " . $fromIndex); } } return ($index != false) ? $index : -1; } public function isEmpty() { return $this->count == 0; } public function lastIndexOf($str, $fromIndex = null) { if ($fromIndex == null) { } else { if ($fromIndex < 0 || $fromIndex >= $this->count) { throw new Exception("String index out of range: " . $fromIndex); } } return ($index != false) ? $index : -1; } public function length() { return $this->count; } public function matches($regex) { } public function regionMatches($offsetA, $str, $offsetB, $length) { if ($str instanceof String == false) { $str = new String($str); } return $this->substring($offsetA, $length)->equals($str->substring($offsetB, $length)); } public function regionMatchesIgnoreCase($offsetA, $str, $offsetB, $length) { if ($str instanceof String == false) { $str = new String($str); } return $this->substring($offsetA, $length)->equalsIgnoreCase($str->substring($offsetB, $length)); } public function replace($old, $new) { } public function replaceAll($regex, $str, $limit = null) { if ($limit == null) { } } public function replaceFirst($regex, $str) { return $this->replaceAll($regex, $str, 1); } public function reverse() { } { } public function startsWith($prefix) { } public function substring($beginIndex, $endIndex = null) { if ($endIndex == null) { $endIndex = $this->count - 1; } if ($beginIndex < 0) { throw new Exception("String index out of range: " . $beginIndex); } if ($endIndex >= $this->count) { throw new Exception("String index out of range: " . $endIndex); } if ($beginIndex > $endIndex) { throw new Exception("String index out of range: " . $endIndex - $beginIndex); } } public function toLowerCase() { } public function toUpperCase() { } { } public function __toString() { return $this->value; } } // EOF: String.php
You need to login to post a comment.
