Shorten a string.

@param int $length
@param bool $truncate
@return string
433642
public function toShorten($length = 32, $truncate = false)
        {
            $string = $this->get();

            if (is_bool($length)) {
                $truncate = $length;
                $length = 32;
            } else {
                $length = (int)$length;
            }

            if (strlen($string) > $length) {
                $length - 4;
                $string = substr($string, 0, $length);
                if (!$truncate) {
                    $string = explode(' ', $string);
                    $size = count($string) - 1;
                    if ($size > -1) {
                        $final = '';
                        for ($i = 0; $i < $size; ++$i) {
                            $final .= $string[$i].' ';
                        }
                        $string = $final;
                    }
                }
                $string = trim($string).'...';
            }

            return $string;
        }
978963
function truncate(input, length, suffix, preserve) {

  length = isUndefined(length) ? input.length : length;
  preserve = preserve || false;
  suffix = suffix || '';

  if(!isString(input) || (input.length <= length)) return input;

  return input.substring(0, (preserve) ?
    ((input.indexOf(' ', length) === -1) ? input.length : input.indexOf(' ', length)) :
    length) + suffix;
}
748161
private function truncateStatus(string $status) : string
    {
        $prefixLength = strlen(Constants::STATUS_PREFIX);
        if (strpos($status, Constants::STATUS_PREFIX) !== 0) {
            return $status;
        }

        return substr($status, $prefixLength);
    }
605205
protected function _truncate()
    {
        if (!$this->getLimit()->isNull()) {
            if (is_null($this->getLimit()->getCount())) {
                $this->_output = array_splice($this->_output, $this->getLimit()->getStart());
            } else {
                $this->_output = array_splice($this->_output, $this->getLimit()->getStart(), $this->getLimit()->getCount());
            }
        }
    }
826774
def truncate(input, length = 50, truncate_string = "...".freeze)
      return if input.nil?
      input_str = input.to_s
      length = Utils.to_integer(length)
      truncate_string_str = truncate_string.to_s
      l = length - truncate_string_str.length
      l = 0 if l < 0
      input_str.length > length ? input_str[0...l] + truncate_string_str : input_str
    end
910787
protected function trim_center($string, $length) {
        $trimlength = ceil($length/2);
        $start = core_text::substr($string, 0, $trimlength);
        $end = core_text::substr($string, core_text::strlen($string)-$trimlength);
        $string = $start.'...'.$end;
        return $string;
    }
594946
def _truncate(p_str, p_repl):
    
    # 4 is for '...' and an extra space at the end
    text_lim = _columns() - len(escape_ansi(p_str)) - 4
    truncated_str = re.sub(re.escape(p_repl), p_repl[:text_lim] + '...', p_str)

    return truncated_str
614170
public static String truncate(String str, int count)
    {
        if(count < 0 || str.length() <= count)
            return str;

        int pos = count;
        for(int i = count; i >= 0 && !Character.isWhitespace(str.charAt(i)); i--, pos--);
        return str.substring(0, pos)+"...";
    }
540207
public static function truncate($value, array $params)
    {
        if (empty($params['length']) || !is_numeric($params['length'])) {
            $params['length'] = 4;
        }

        return StringHelper::truncate($value, (int)$params['length']);
    }
1369995
public function truncate($value, $length, $replace = '...')
    {
        if (!isset($value)) {
            return null;
        }
        
        return strlen($value) <= $length ? $value : substr($value, 0, $length - strlen($replace)) . $replace;
    }
84679
def truncatechars(value, arg):
    
    try:
        length = int(arg)
    except ValueError: # Invalid literal for int().
        return value # Fail silently.
    if len(value) > length:
        return value[:length] + '...'
    return value
1180872
public static String truncate (String text, int length, String append)
    {
        return StringUtil.truncate(text, length, append);
    }
816615
public function truncate($message, $length, $suffix = '...')
    {
        $computedLength = $length - $this->strlen($suffix);

        if ($computedLength > $this->strlen($message)) {
            return $message;
        }

        if (false === $encoding = mb_detect_encoding($message, null, true)) {
            return substr($message, 0, $length).$suffix;
        }

        return mb_substr($message, 0, $length, $encoding).$suffix;
    }
556696
public static function java_truncate(string $str): string
    {
        if (strlen($str) > 955) {
            return substr($str, 0, 475).'[...]'.substr($str, -475);
        }

        return $str;
    }
1037248
def _truncate(value, arg):
        
        length = int(arg)
        if value is None:
            value = ""
        if len(value) > length:
            return value[:length] + "..."
        else:
            return value
633682
def truncate(string, index):
    """"""
    if len(string) > index and index > 0:
        string = string[:index - 1] + u('…')
    return string
1070671
public static function truncate($str, $maxlength=20)
    {

        if(strlen($str) > $maxlength){
            $str = substr($str, 0, $maxlength-3).'...';
        }

        return $str;
    }