-2

how to delete (or hide) characters in a string, for example: I have string with 20 characters and I need to delete all characters from 15. P.S. I got one idea, but don't think that its correct: create two paragraphs with different classes, one paragraph with 15 characters, second one doesn't matter and delete/hide second paragraph example:

    var count;
    count = $('#mydiv').text().length;
    if (count => 15) { 
    }
Bumblebee
  • 11
  • 1
  • Hard to make much sense out of what exactly it is you are trying to accomplish.what your html looks like and what expected results are. Please take a few minutes to read [ask] and [mcve] – charlietfl Jun 01 '17 at 12:57
  • 1
    have a look at [substr](https://www.w3schools.com/jsref/jsref_substr.asp) – myfunkyside Jun 01 '17 at 12:58
  • 2
    Dupe of https://stackoverflow.com/questions/952924/javascript-chop-slice-trim-off-last-character-in-string – j08691 Jun 01 '17 at 12:58

6 Answers6

1

You are looking for the substring() method.

var text = "this is a super long string.";
var shortText = text.substring(0, 15);
// shortText now is "this is a super"
finngu
  • 457
  • 4
  • 23
0

Use string.substr

var str = "Hello world!";
var res = str.substr(0, 5); // returns Hello
Zezima
  • 17
  • 4
0

Use the slice function - string.slice(start, end)

var res = str.slice(0, 16);

0

Via CSS:

https://css-tricks.com/snippets/css/truncate-string-with-ellipsis/

.truncate {
  width: 250px; // or any other width
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Via lodash:

_.truncate('hi-diddly-ho there, neighborino');
// => 'hi-diddly-ho there, neighbo...'

_.truncate('hi-diddly-ho there, neighborino', {
  'length': 24,
  'separator': ' '
});
// => 'hi-diddly-ho there,...'

_.truncate('hi-diddly-ho there, neighborino', {
  'length': 24,
  'separator': /,? +/
});
// => 'hi-diddly-ho there...'

_.truncate('hi-diddly-ho there, neighborino', {
  'omission': ' [...]'
});
// => 'hi-diddly-ho there, neig [...]'
vsync
  • 118,978
  • 58
  • 307
  • 400
0

If you know what you want to remove then just replace it with .replace() not sure exactly what you mean by hide characters. It seems all answers are possible solutions to your question though https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

0

I suggest you take a look at the substring support page:
https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/String/substring

jsfiddle with substring use:
https://jsfiddle.net/zdLdk6Lj/

var divtext = $('#mydiv').text();

$('#mydiv').text(divtext.substring(0,15));
Peter-Paul
  • 118
  • 5