What is the difference between size and length of A String in general programing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the difference between size and length of A String in general programing

Size and length are two different parts if String .what each do mean and what differes them

23rd May 2019, 3:25 AM
Ayush Pandey
Ayush Pandey - avatar
17 Answers
+ 1
In C language, SIZE- bytes occupied by an variable,ie.,int x = 10,in an 32 bit compiler is of size 4bytes... LENGTH - number of each components in it. ie.,char *a = "Nandha"; is of length 6 and of SIZE is 2bytes,as all pointer variables are of 2bytes whether it's char,int or float.
8th Jun 2019, 8:23 AM
Nandhakumar
Nandhakumar - avatar
+ 17
String Length The length of a string is the number of characters that it contains. To obtain this value, call the length( ) method, shown here: int length( ) The following fragment prints "3", since there are three characters in the string s: char chars[] = { 'a', 'b', 'c' }; String s = new String(chars); System.out.println( s.length() );
23rd May 2019, 11:58 AM
MeanMachine
MeanMachine - avatar
+ 13
Ayush Pandey String Literals The earlier examples showed how to explicitly create a String instance from an array of characters by using the 'new' operator. However, there is an easier way to do this using a string literal. https://code.sololearn.com/cjI33nLA456Q/?ref=app
23rd May 2019, 3:51 PM
MeanMachine
MeanMachine - avatar
+ 5
what is the difference between size() and length() function...😉😉😉😉 https://www.codeproject.com/Questions/453779/what-is-the-difference-between-size-and-length-fun
23rd May 2019, 3:29 AM
Asmit joy
Asmit joy - avatar
+ 5
Difference is absolutely language and library dependant, as a user you have the role to read the reference for your specific language and and method. As a programmer you have the role to understand the reference. Sad but true.
23rd May 2019, 9:34 AM
AZTECCO
AZTECCO - avatar
+ 4
From a public interface overview they are just names given to functions, there should be always one name for a particular purpose but we have more than one, this can lead to confuse clients, in fact size in programming is usually associated to the binary size of an element. Probably developers hadn't defined a convention when these libraries were made.. Anyway when you refer to a type container the size in bytes doesn't matter ,(it always do for performance indeed) what I mean is ... A type encapsulate and abstract internal behavior and give you only what a client needs and who use a string type doesn't matter what size in bytes is
23rd May 2019, 7:29 AM
AZTECCO
AZTECCO - avatar
+ 4
Well sorry if it sounded complex, didn't mean to make it complex but since you tagged many languages the answer becomes somewhat broader by scope; each language you mentioned has different ways of working with string. So can you describe me which of my previous response was difficult to understand? maybe I can try in another way to explain. But I need to know what to focus on, since I don't know all languages.
23rd May 2019, 8:54 AM
Ipang
+ 4
Well, irrespective of languages, I think you can picture a string as a chain of characters; that's the best I could think of being irrespective of language. Details are of course language specific.
23rd May 2019, 9:23 AM
Ipang
+ 4
AZTECCO It's not sad bro! it's just true 😁
23rd May 2019, 9:37 AM
Ipang
+ 3
For C++ std::string, the `size` and `length` method are equal, they both return the number of characters in the string, but I personally prefer `size`, because the method name is also used in some other STL container, while `length` is (AFAIK) only implemented in std::string (cmiiw)
23rd May 2019, 5:19 AM
Ipang
+ 3
It really isn't that simple, as I said, it depends which language you ask. For example, C string are character array that uses a null terminator to denote the string endpoint. In C++ std::string the internal buffer is not null terminated, and it was a class - more than an array of characters. I don't know how C#, Java, Python string works internally, but I once read Python's variable size in memory is larger than other languages (if I understood that correctly)
23rd May 2019, 5:43 AM
Ipang
+ 2
Well, that wasn't explained clearly in the original question. I think this is a language dependent question, I think you meant to ask about size of a string variable in memory, and the length of the string. I suggest you to revise your original question so not to confuse people.
23rd May 2019, 5:35 AM
Ipang
+ 2
Still not getting it to be honest you all are explaining in quite complicated manner. I needed to know just as user manner size and length.
23rd May 2019, 8:42 AM
Ayush Pandey
Ayush Pandey - avatar
+ 2
What about size MeanMachine is it the memory allocated to a data type
23rd May 2019, 12:03 PM
Ayush Pandey
Ayush Pandey - avatar
+ 1
No i am not talking about function size and length . I am talking of general length of a string and size of a string Ipang Asmit Das Joy
23rd May 2019, 5:28 AM
Ayush Pandey
Ayush Pandey - avatar
+ 1
Size equals to memory occupied and Length equals to no of characters right??
23rd May 2019, 5:37 AM
Ayush Pandey
Ayush Pandey - avatar
+ 1
Ipang well i need ans as user's normal prospective irrespective of languages as a basic concept of programing.
23rd May 2019, 8:57 AM
Ayush Pandey
Ayush Pandey - avatar