How to find the length of a string

Just out of being bored I decided to share some really basic source code. Here’s how to find the lenght of a text in my three favorite languages:

PHP

$s = "simple php programming";
$length = strlen($s);

JavaScript

var s = "simple javascript programming";
var length = s.length;

(Visual) Basic

dim s as string
dim length as integer

s = "simple basic programming";
length = len(s);

5 comments so far

  1. drj11 on

    The JavaScript version has a trivial bug. It should be «var length = s.length». length is a straightforward property, not a method to be invoked.

  2. michiel on

    Thanks. Not only was I bored, also confused ;-)

  3. [...] find out if a variable has been defined Posted by michiel under Programming   It’s two articles now, it must be a series! Here’s how to find the out if a variable has been defined [...]

  4. [...] Programming, Uncategorized | Tags: programming php javascript |   It’s three articles (1 2) now, it’s almost a book! Here’s how to create a class in my three favorite [...]

  5. Slava on

    If a string is Unicoded, how many bytes does string fit?
    Any idea for Javascript solution?


Leave a reply