Returns a substring that contains the n leftmost characters of the string.
The entire string is returned if n is greater than size() or less than zero.

@param int $n
@return null|MString
775846
public function left($n)
    {
        if ($n < 0 || $n > $this->size()) {
            return new MString($this->text);
        }

        $result = substr($this->text, 0, $n);

        if ($result === false) {
            return null;
        }

        return new MString($result);
    }
418409
public function startsWith($substring) {
		$substringLength = mb_strlen($substring, $this->encoding);
		$startOfStr = mb_substr($this->string, 0, $substringLength, $this->encoding);

		return (string) $substring === $startOfStr;
	}
760799
public function substring($start, $end = null)
    {
        if ($end === null) {
            return substr($this->content, $start);
        }
        $len = $end - $start;
        if ($len < 0) {
            return '';
        }
        return substr($this->content, $start, $len);
    }
1373939
private function select_string($n) {
        $string = $this->get_plural_forms();
        $string = str_replace('nplurals', "\$total", $string);
        $string = str_replace("n", $n, $string);
        $string = str_replace('plural', "\$plural", $string);

        $total = 0;
        $plural = 0;

        eval("$string");
        if ($plural >= $total) $plural = $total - 1;
        return $plural;
    }
442941
public function substring($start, $length = NULL) {
        //TODO: Fix to PHP #42101
        if($length === NULL) {
            $newValue = mb_substr($this->string, $start);
        } else {
            $newValue = mb_substr($this->string, $start, $length);
        }

        return $this->createClone($newValue);
    }
116556
public function mbStrLen($string, $encoding = null)
    {
        // Meh, the original mb_strlen interprets a null here as an empty string.
        if ($encoding === null) {
            return mb_strlen($string);
        }
        return mb_strlen($string, $encoding);
    }
163396
public function writeShort($n)
    {
        if ($n < 0 || $n > 65535) {
            throw new \InvalidArgumentException('Octet out of range 0..65535');
        }

        $this->flushBits();
        $this->out .= pack('n', $n);
        return $this;
    }
331101
public function first($n)
    {
        $first = '';

        if ($n > 0)
        {
            $first = UTF8::substr
            (
                $this->scalarString,
                0,
                $n,
                $this->encoding
            );
        }

        return $this->newSelf($first);
    }
186025
public function length($min = null, $max = null)
	{
		$lastSegmentKey = $this->getLastSegmentKey();

		if (!is_null($min) && !is_null($max) && $max > $min)
		{
			$lengthPattern = sprintf('{%d,%d}', (int) $min, (int) $max);
		}
		elseif (!is_null($min) && !$max)
		{
			$lengthPattern = sprintf('{%d}', (int) $min);
		}
		else
		{
			$lengthPattern = '{1}';
		}

		return $this->replaceQuantifierByKey($lastSegmentKey, $lengthPattern);
	}
652431
public function look($n, $o = 0)
    {
        if ($this->length <= $o) {
            return '';
        }
        return $this->substr($o, $n);
    }
774391
public static function maxStrlen(array $string)
    {
        $length = 0;
        foreach ($string as $str) {
            if (strlen($str) > $length) {
                $length = strlen($str);
            }
        }

        return $length;
    }
122216
protected static function array_nth(array $a, $n) {
        $keys = array_keys($a);
        if ($n < 0 or $n > count($keys) - 1) {
            return null;
        }
        $key = $keys[$n];
        return $a[$key];
    }
640513
public function objectKey($n)
	{
		if ($this->useRC128Encryption) {
			$len = 16;
		} else {
			$len = 10;
		}

		return substr($this->md5toBinary($this->encryptionKey . pack('VXxx', $n)), 0, $len);
	}
383970
public function last($n)
    {
        $last = '';

        if ($n > 0)
        {
            $last = UTF8::substr
            (
                $this->scalarString,
                -$n,
                null,
                $this->encoding
            );
        }

        return $this->newSelf($last);
    }
174595
public function substring($start, $end = null)
	{
		$start = (int) $start;
		$end   = $end !== null ? (int) $end : null;

		if ($start < 0 || $start >= $this->length() || $end !== null && ($end < 0 || $end >= $this->length())) {
			throw new OutOfBoundsException();
		}

		$string = mb_substr($this->string, $start, $end !== null ? $end + 1 : $this->length(), $this->encoding);
		$stringBuilder = new StringBuilder($string);
		$stringBuilder->setEncoding($this->encoding);
		return $stringBuilder;
	}
1128964
public static function substr($string, $from, $length = null)
    {
        if (false === $encoding = mb_detect_encoding($string, null, true)) {
            return substr($string, $from, $length);
        }

        return mb_substr($string, $from, $length, $encoding);
    }
1176034
function _substr($string, $start, $length = null) {
        static $mbstring;

        // Cache expensive function_exists() calls.
        if (!isset($mbstring)) {
            $mbstring = function_exists('mb_substr');
        }

        if (is_null($length)) {
            $length = $this->_strlen($string);
        }
        if ($mbstring) {
            $ret = @mb_substr($string, $start, $length, $this->_charset);
            if (!empty($ret)) {
                return $ret;
            }
        }
        return substr($string, $start, $length);
    }