Home > OS >  Javascript Substring not working with special characters
Javascript Substring not working with special characters

Time:11-09

Hi there I'm trying to use the following

sValue.substr(0,1000);

on the string

<ul class='ulIndentRight'><li><b>Altissimo Guesthouse</b> </li><li> Luxury Room With Queen Bed Rtid 1 </li><li> Extra</li><li>person Charges May App Superior Room 1 Queen Bedlayout </li><li> Bedroom Internet </li><li> Free Wifi Entertainment </li><li> Plasma Tv</li><li> Satellite Channels</li><li> And Dvd Playerfood </li><li> Drink </li><li> Refrigerator</li><li> Free Minibar Items</li><li> Coffee</li><li>tea Maker</li><li> And Free Bottled Waterbathroom </li><li> Private Bathroom</li><li> Bathtub Or Shower</li><li> Free Toiletries</li><li> And A Hair Dryerpractical </li><li> Safe</li><li> Desk</li><li> And Iron</li><li>ironing Board (on Request) Rollaway</li><li>extra Beds Available On Requestcomfort </li><li> Climate</li><li>controlled Air Conditioning And Daily Housekeepingnon</li><li>s

but it keeps on returning a character count of 1136

any ideas how to get only 1000 characters

Thanks

CodePudding user response:

1136 is the length of your entire string.

I suspect that you are not using the return value from substr.

You will need to assign the return value and use that instead as substr does not modify the original value.

sValue.substr(0,1000);
// sValue.length => 1136

const newString = sValue.substr(0,1000);
// newString.length => 1000

CodePudding user response:

let value = "Paste_whole_string_in_here"
console.log(value.substring(0,1000))

This is working for me.

CodePudding user response:

That's HTML special characters.

I think the easiest way:

  1. you create a temporary element
  2. you put your string in the HTML of the element (innerHTML)
  3. you put the characters from this element with textContent in new string
  4. you make the count on this string

let string = 
        "<ul class='ulIndentRight'><li><b>Altissimo Guesthouse</b> </li><li> Luxury Room With Queen Bed Rtid 1 </li><li> Extra</li><li>person Charges May App Superior Room 1 Queen Bedlayout </li><li> Bedroom Internet </li><li> Free Wifi Entertainment </li><li> Plasma Tv</li><li> Satellite Channels</li><li> And Dvd Playerfood </li><li> Drink </li><li> Refrigerator</li><li> Free Minibar Items</li><li> Coffee</li><li>tea Maker</li><li> And Free Bottled Waterbathroom </li><li> Private Bathroom</li><li> Bathtub Or Shower</li><li> Free Toiletries</li><li> And A Hair Dryerpractical </li><li> Safe</li><li> Desk</li><li> And Iron</li><li>ironing Board (on Request) Rollaway</li><li>extra Beds Available On Requestcomfort </li><li> Climate</li><li>controlled Air Conditioning And Daily Housekeepingnon</li><li>s";
        
            document.querySelector('.string').innerHTML = string;

  let string_div = document.querySelector('.string').textContent;
  charCount = Array.from(string_div).length;
  document.querySelector('.count').innerHTML = charCount;
  console.log(charCount);
<div  style="display:none">
</div>

<div >
</div>

  • Related