{ }codelper

Learn › JavaScript

Data · 09

Strings

Length, uppercase, reverse, and picking one character.

A string is a row of characters.
The example below reads one word and prints its length, its uppercase form, the word reversed, and its first character.

const s = require("fs").readFileSync(0, "utf8").trim();console.log(s.length);console.log(s.toUpperCase());console.log([...s].reverse().join(""));console.log(s[0]);
stdin
hello
stdout
5
HELLO
olleh
h

Key points

  • Indexes start at 0.
    The first character is s[0] and the last is at length - 1.
  • Tools such as length, uppercase, reverse, split and trim exist in every language; only the names differ.
  • Taking part of a string splits languages in two.
    Where you pass an end index, (1, 3) on "abcdef" gives "bc"; where you pass a count, it gives "bcd".
  • Underneath, a character is a number (its character code): A is 65, a is 97.
    In C, C++, Java and C#, a char is simply a number, so 'A' + 1 is 66.
  • Except in C and C++, strings can't be modified.
    To change one, build a new string.

How each language writes it

PythonLength len(s), uppercase s.upper(), reverse s[::-1], split s.split(), trim s.strip(), to number int(s), slice s[1:3] (end index), codes ord("A") → 65, chr(97) → "a"
JavaScriptLength s.length, uppercase s.toUpperCase(), reverse [...s].reverse().join("") (strings have no reverse), s.split(" "), s.trim(), slice s.slice(1, 3) (end index), codes s.charCodeAt(0), String.fromCharCode(97)
TypeScriptSame as JavaScript
JavaLength s.length(), one character s.charAt(i) (not s[i]), uppercase s.toUpperCase(), reverse new StringBuilder(s).reverse(), s.split(" "), s.trim(), slice s.substring(1, 3) (end index)
C#Length s.Length, uppercase s.ToUpper(), reverse with Array.Reverse on the array from ToCharArray(), s.Split(' '), s.Trim(), slice s.Substring(1, 3) (count).
Turn a code back into a character with (char)
C++Length s.size() (or s.length()), uppercase one character at a time with toupper, reverse std::string(s.rbegin(), s.rend()), slice s.substr(1, 3) (count)
CLength strlen(s) — it scans to the closing \0 every time.
Uppercase one character at a time with toupper; reverse with a backwards loop
CraykonLength s.Length, uppercase s.ToUpper() (lowercase ToLower), reverse s.Reverse(), s.Split(" "), s.Trim(), to number s.ToNumber(), slice s.Substring(1, 3) (count), codes Str.Code("A"), Str.FromCode(97)

Common mistakes

Changing one character of a stringPython · JavaScript · TypeScript · Craykon

s = "abc"
s[0] = "x"

→ Python raises TypeError, JavaScript silently ignores it, TypeScript gives a compile error, and Craykon reports that strings can't be changed one character at a time

Build a new string, e.g. Python "x" + s[1:].

Counting Korean text in bytesC · C++

strlen("한글")

→ 6, not 2 — it counts bytes, and in UTF-8 one Korean character is 3 bytes (C++ std::string("한글").size() is also 6)

In C and C++, remember that bytes and characters are not the same count.
Python, Java, C#, JavaScript and Craykon say 2.

How languages differ
To check whether two strings are equal in Java, use s.equals("yes"), not == (== checks for the same object, so comparing input you read with "yes" gives false).
In C, == compares addresses too, so use strcmp(a, b) == 0; Python, JavaScript, C#, C++ (std::string) and Craykon compare contents with ==.

Practice