Home >Web Front-end >JS Tutorial >How to truncate a string in JavaScript_javascript tips

How to truncate a string in JavaScript_javascript tips

WBOY
WBOYOriginal
2016-05-16 15:50:171194browse

The example in this article describes how to truncate strings in JavaScript. Share it with everyone for your reference. The details are as follows:

Here JavaScript truncates the string, similar to substr(), except that this function does not truncate the word. After truncation occurs, an ellipsis will be added

if (!function_exists('subsent')) {
  function subsent($string, $start = 0, $length = 0, $cap = '...') {
   if ($length <= 0 || strlen($string) < $length) { return $string; }
   $string = substr($string, $start, strpos($string, ' ', $length)) . ' ' . $cap;
   return $string;
  }
}

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn