3Data: Numbers and Strings
Developing and mastering the natural language has brought humankind a decisive advantage on our planet. Interpreting sounds or written signs is the first step. For a computer, this interpretation is not as soft and flexible as it is for human beings. In any real application, the first obstacle when reading a number or a piece of text is to correctly include it into the processing flow.
A single sign may mean different things: figures 7 and 10 can be numbers, but in “Ronaldo wears the 7, and Maradona the 10”, it is just nonsense to add 7 and 10.
Different words may mean the same thing: “Général De Gaulle” and “Charles de Gaulle”, or simply “Jean Pierre” and “Jean-Pierre”.
An illustration is given with the electoral data in Part 3: the difficulties in identifying a same single candidate when the name can be differently written out in two different files.
In this chapter, we visit “numbers” and “strings”, and in particular the use of:
- – concatenation operator (+), and comparisons with type string variables;
- – methods of the object
StringandString.prototype; - – regular expressions and the object
RegExp.
3.1. Handling numbers
3.1.1. Literal notation of type “number” variables
The literal notation of numbers can take several forms:
4.2 encodes the real number 4.2-3e-5 encodes -0.00003 (scientific notation)0x0f encodes 15 (hexadecimal notation)0x0g SyntaxError: identifier starts immediately after numeric literal// only characters [0-9, a-f] ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access