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
Release: 2016-05-16 15:50:17
Original
1188 people have browsed it

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;
  }
}

Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template