Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

String length: Difference between revisions

From Coderwiki
new: info, null termination, checking a string's length
 
add: C Sharp language-specific guide
 
Line 10: Line 10:
== Checking a string's length ==
== Checking a string's length ==
In most programming languages, there is either a [[function]], [[method]] or [[field]] which returns the length of the string.
In most programming languages, there is either a [[function]], [[method]] or [[field]] which returns the length of the string.
Guides for specific programming languages:
* [[C Sharp string.Length]]

Latest revision as of 11:21, 15 August 2025

Most programming languages (with the notable exception of C) have a way to check the number of elements in a collection.

As strings are just collections, this method applies to strings too.

Null termination

edit edit source

In many languages, strings are null terminated. This means that if you have a 5-character string, it will actually be a collection of 6 elements (as it includes the \0 null-terminator).

When checking a string's length in most programming languages, this null terminator is ignored, so the length will come out as 5 in the example above.

Checking a string's length

edit edit source

In most programming languages, there is either a function, method or field which returns the length of the string.

Guides for specific programming languages: