Languages
[Edit]
EN

JavaScript - simple way how to write own function to make beauty string shortcuts

7 points
Created by:
Selina-Miranda
737

In this article, we're going to have a look at how to write in JavaScript own function that creates shortcuts for string.

Beauty string shortcuts created with JavaScript.
Beauty string shortcuts created with JavaScript.

There are available styles to achieve the same effect, but sometimes we are not able to do it with styles and we need to have shortcut string e.g canvas string drawing or title / alt attribute.

This approach solves somehow the problem of multiline text with ... in the end, if we don't want to display all text because CSS solutions are not enough at this moment (we have 2020).

1. Custom method example

Edit

The presented solution in this section tries to find closes white characters to cut the string in a pretty way - to do not split worlds. Only when it is impossible to find the white characters, function cuts worlds.

ES6+ version

Edit

 

Optimal solution

Edit

 

See also

Edit
  1. CSS - cut with dots (...) overflowing text outside element

  2. JavaScript - create HTML shortcut

Alternative titles

  1. JavaScript - create string shortcut
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - String (popular problems)

JavaScript - write own function to make beauty string shortcuts
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join