0

Hi I have list of title in a drop-down as string ex:

"Bharat Untitled offer #564", "Bharat Untitled offer #563" 

respectively.

Its title is long so, We want title should show first eight character, then '...' and last 4 character.

I used css rules:

.title{
    max-width: 100px;
    min-width: 100px;
    overflow: hidden;
    text-overflow:ellipsis;
}

And get output like:

Bharat Unt...

I want out put like:

Bharat Untitled...ffer

Any ideas how to do this?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Bharat Bhushan
  • 305
  • 1
  • 14

3 Answers3

1

Try this:

var charAtBeginning = 10;
var charAtEnd = 7;
var myStr = "I have list of title in a drop-down as string example";
var finalOutput = "";
if (myStr.length > (charAtBeginning + charAtEnd)) {
  finalOutput = myStr.substring(0, charAtBeginning) + '..' + myStr.substring((myStr.length - charAtEnd), myStr.length);
}
alert(finalOutput);
Rayon
  • 36,219
  • 4
  • 49
  • 76
0

if you want to do it with javascript or jquery then there are certain possible ways to do this thing, i am going to illustrate on menthod here.

   function processName(ret){
    ret = ret.substr(0,8) + "..."+ret.substr(ret.length - 5);
    return ret;
   }

now All you need to do is to pass each and every value to this function, i assume you know how to do it. good luck.

Siddharth
  • 859
  • 8
  • 16
0

https://jsfiddle.net/8ye6df9p/

I made this little thing.. i used a monospace font.. hopes it make you bit wiser..

span {
  font-family: "Lucida Console", Monaco, monospace;
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 6em;
  font-size: 1em;
}
<span>dog eatsdodddddddddgsdfghjkghersdddgsdfghjkghersdddgsdfghjkghersdddgsdfghjkgherstyfghurefyghujrttgyhujrttgyhjretghrtgh</span>

<span>dogfgggyfg ff tgyhujrttgyhjretghrtgh</span>

<span>dog </span>

<span>brainatgs200 </span>