@name truncate
@kind function

@description
truncates a string given a specified length, providing a custom string to denote an omission.
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;
}
1316185
public static function truncateText($text, $length = 30, $truncate_string = '...', $truncate_lastspace = false)
    {
        if ($text == '') {
            return '';
        }

        $mbstring = extension_loaded('mbstring');
        if ($mbstring) {
            $old_encoding = mb_internal_encoding();
            mb_internal_encoding(mb_detect_encoding($text));
        }
        $strlen = ($mbstring) ? 'mb_strlen' : 'strlen';
        $substr = ($mbstring) ? 'mb_substr' : 'substr';
        if ($strlen($text) > $length) {
            $truncate_text = $substr($text, 0, $length - $strlen($truncate_string));
            if ($truncate_lastspace) {
                $truncate_text = preg_replace('/\s+?(\S+)?$/', '', $truncate_text);
            }
            $text = $truncate_text.$truncate_string;
        }
        if ($mbstring) {
            mb_internal_encoding($old_encoding);
        }

        return $text;
    }
1234879
function truncateString(str, block_size) {
    block_size = block_size || 350;

    var blocks = [],
        current_pos;

    for (current_pos = 0; current_pos < str.length; current_pos = current_pos + block_size) {
        blocks.push(str.substr(current_pos, block_size));
    }

    return blocks;
}
113025
function truncate(arr, len) {
    if (arr.length > len) {
      arr.length = len;
      return true;
    }
  }
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
267581
public boolean truncate(long length)
    throws IOException
  {
    if (length == 0) {
      if (exists()) {
        StreamImpl stream = openWriteImpl();
        stream.close();

        clearStatusCache();

        return true;
      }
      else
        return false;
    }
    else
      throw new UnsupportedOperationException(getClass().getName() + ": truncate");
  }
1226827
private static String truncateWS(String pStr) {
        char[] chars = pStr.toCharArray();

        int count = 0;
        boolean lastWasWS = true; // Avoids leading WS
        for (int i = 0; i < chars.length; i++) {
            if (!Character.isWhitespace(chars[i])) {
                // if char is not WS, just store
                chars[count++] = chars[i];
                lastWasWS = false;
            }
            else {
                // else, if char is WS, store first, skip the rest
                if (!lastWasWS) {
                    if (chars[i] == 0x0d) {
                        chars[count++] = 0x0a; //Always new line
                    }
                    else {
                        chars[count++] = chars[i];
                    }
                }
                lastWasWS = true;
            }
        }

        // Return the trucated string
        return new String(chars, 0, count);
    }
666240
function trimToMaxWidth (width, text) {
	var truncated = text.split('\n').map(function (line) {
		return line.substring(0, width);
	});
	return truncated.join('\n');
}
1151225
public function truncateText($text = null, $length = 0) {
		$result = '';
		if (empty($text)) {
			return $result;
		}

		$text = (string)$text;
		$length = (int)$length;
		if ($length <= 0) {
			$length = 50;
		}

		if (($text === h($text)) && (mb_strlen($text) <= $length)) {
			return $text;
		}

		$truncateOpt = $this->_getOptionsForElem('truncateText.truncateOpt');
		$tuncatedText = $this->Text->truncate($text, $length, $truncateOpt);
		if ($tuncatedText === $text) {
			return $tuncatedText;
		}

		$result = $this->Html->div('collapse-text-truncated', $tuncatedText);
		$result .= $this->Html->div(
			'collapse-text-original',
			$text . $this->_getOptionsForElem('truncateText.collapseLink')
		);
		$result = $this->Html->div('collapse-text-expanded', $result);

		return $result;
	}
1270482
public static function truncate(string $string, int $maxLength, string $indicator = '...'): string
    {
        if (mb_strlen($string) > $maxLength) {
            $string = mb_substr($string, 0, $maxLength - mb_strlen($indicator)) . $indicator;
        }

        return $string;
    }
415992
function truncFilename(path, rootFolderName) {
    // bail if a string wasn't provided
    if (typeof path !== 'string') {
        return path;
    }

    var index = path.indexOf(rootFolderName);
    if (index > 0) {
        return path.substring(index + rootFolderName.length + 1);
    } else {
        return path;
    }
}
727235
public static function truncateSafe($string, $length, $append = '...'): string
    {
        $result = mb_substr($string, 0, $length);
        $lastSpace = mb_strrpos($result, ' ');

        if ($lastSpace !== false && $string !== $result) {
            $result = mb_substr($result, 0, $lastSpace);
        }

        if ($result !== $string) {
            $result .= $append;
        }

        return $result;
    }
216536
public static String truncate(String string, int maxBytesLength,
            String appendString) {
        return string.getBytes().length > maxBytesLength ? truncate(string,
                maxBytesLength - appendString.getBytes().length) + appendString
                : string;
    }
216405
def truncate_file(f):
    
    if isinstance(f, InMemoryUploadedFile):
        f.file = StringIO()
    else:
        f.seek(0)
        f.truncate(0)
962691
def truncate(str, len = 80)
      if str and str.length > len and (add = str[len..-1].index(' '))
        str = str[0, len + add] + '&#8230;'
      end
      str
    end
829465
public function truncate(int $length = 100, string $append = '...') : Manipulator
    {
        return new static(mb_substr($this->string, 0, $length) . $append);
    }
104591
def truncate(v: str, *, max_len: int = 80) -> str:
    
    if isinstance(v, str) and len(v) > (max_len - 2):
        # -3 so quote + string + … + quote has correct length
        return repr(v[: (max_len - 3)] + '…')
    v = repr(v)
    if len(v) > max_len:
        v = v[: max_len - 1] + '…'
    return v