How do you get the length of a string?
Asked Answered
N

10

264

How do you get the length of a string in jQuery?

Nabob answered 25/6, 2009 at 13:59 Comment(1)
Excellent just use normal JS.Nabob
R
499

You don't need jquery, just use yourstring.length. See reference here and also here.

Update:

To support unicode strings, length need to be computed as following:

[..."𠮷"].length

or create an auxiliary function

function uniLen(s) {
    return [...s].length
}
Rockie answered 25/6, 2009 at 14:0 Comment(7)
I was too, oh the joys of SO.Arenaceous
I bet that's just competition. :DEmelyemelyne
Artem Barger -- probably because he asked about using Jquery.Thibaud
Artem Barger - probably because he was looking for the same thing as I was: a way to get the length of text contained in an HTML tag. I'm a beginner so I knew how to get the content but not its length. In that sense, genesis' answer below is better.Untimely
"𠮷".length == 2. How can we get the actual number of characters?Bes
to make unicode aware lenght you need to run it following way: [..."𠮷"].lengthRockie
"You don't need jquery..." - that's not what the OP asked.Quanta
R
213

The easiest way:

$('#selector').val().length
Reprint answered 14/2, 2010 at 20:50 Comment(4)
the only answer that answers the question as asked.Landgrabber
how is that? no where in the question does he say anything about the string coming from an :input valueCrossfertilization
so does this one: len = $('#selector').is('div') ? "someString".length : 0 and makes just as much sense...Crossfertilization
but this still just uses javascript doesn't it? same as @Artem Barger's answerOxford
R
44

jQuery is a JavaScript library.

You don't need to use jQuery to get the length of a string because it is a basic JavaScript string object property.

somestring.length;
Rapturous answered 25/6, 2009 at 14:2 Comment(1)
+1 for including the line 'jQuery is a JavaScript library.' Lots of answers are saying you don't need jQuery, but this may be confusing to a person who thinks that 'jQuery' and 'JavaScript' are two different things.Greasepaint
G
22

HTML

<div class="selector">Text mates</div>

SCRIPT

alert(jQuery('.selector').text().length);

RESULT

10

Gardal answered 19/11, 2010 at 2:10 Comment(0)
A
22

A somewhat important distinction is if the element is an input or not. If an input you can use:

$('#selector').val().length;

otherwise if the element is a different html element like a paragraph or list item div etc, you must use

$('#selector').text().length;
Anemia answered 16/5, 2012 at 18:22 Comment(1)
It's right Answer for this Question! Here is really defined, how gets a length of "input field" or "text" value. Additional it's possible to get html length, like $('#selector').html().length.Pascasia
L
21

You don't need to use jquery.

var myString = 'abc';
var n = myString.length;

n will be 3.

Legpull answered 25/6, 2009 at 14:1 Comment(0)
A
14

It's not jquery you need, it's JS:

alert(str.length);
Arenaceous answered 25/6, 2009 at 14:0 Comment(0)
C
13

same way you do it in javascript:

"something".length

Crossfertilization answered 25/6, 2009 at 14:0 Comment(0)
K
4

In some cases String.length might return a value which is different from the actual number of characters visible on the screen (e.g. some emojis are encoded by 2 UTF-16 units):

MDN says: This property returns the number of code units in the string. UTF-16, the string format used by JavaScript, uses a single 16-bit code unit to represent the most common characters, but needs to use two code units for less commonly-used characters, so it's possible for the value returned by length to not match the actual number of characters in the string.

In Unicode separate visible characters are called graphemes. In case you need to account for this case, you'll need some lib that can split the string into graphemes, such as this: https://www.npmjs.com/package/grapheme-splitter

Kennie answered 1/4, 2019 at 18:29 Comment(0)
K
2

In jQuery :

var len = jQuery('.selector').val().length; //or 
( var len = $('.selector').val().length;) //- If Element is Text Box

OR

var len = jQuery('.selector').html().length; //or
( var len = $('.selector').html().length; ) //- If Element is not Input Text Box

In JS :

var len = str.length;
Kao answered 17/4, 2015 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.