diff --git a/modules/25-strings/10-quotes/en/EXERCISE.md b/modules/25-strings/10-quotes/en/EXERCISE.md index 27e2695..aa40058 100644 --- a/modules/25-strings/10-quotes/en/EXERCISE.md +++ b/modules/25-strings/10-quotes/en/EXERCISE.md @@ -1,8 +1,8 @@ Write a program that displays: -text +```text "Khal Drogo's favorite word is "athjahakar"" ``` -The program should display this phrase exactly. Note the quotes at the beginning and at the end of a phrase. +The program should display this phrase exactly. Note the quotes at the beginning and at the end of the phrase. diff --git a/modules/25-strings/10-quotes/en/README.md b/modules/25-strings/10-quotes/en/README.md index 48b0d99..bcb615d 100644 --- a/modules/25-strings/10-quotes/en/README.md +++ b/modules/25-strings/10-quotes/en/README.md @@ -1,37 +1,107 @@ +Strings in Java appear at every step. We use them to work with text, print messages to the screen, process user input, and exchange data with external systems. + +From Java's point of view, a string is a set of characters enclosed in double quotes. Let's look at some examples. ```java "Hello" "Goodbye" - "G" " " "" ``` -Which of these five options are strings? +All of these are strings. + +- `"Hello"` and `"Goodbye"` are strings of several characters +- `"G"` is a string of a single character +- `" "` is a string of a single space +- `""` is an empty string, it contains no characters at all. It plays the same role as 0 in mathematics + +Everything inside the quotes is considered a string, even if it's just a space or nothing at all. + +If you print these strings to the screen, `"Hello"` and `"Goodbye"` will be clearly visible. But `" "` and `""` are confusing. Printing an empty string looks like a complete absence of text, while a string with a space shows an "empty spot" that is visually hard to tell apart from emptiness. Java clearly distinguishes between these cases. An empty string means the absence of characters, while a string with a space contains a specific space character. + +A control question. Are these the same strings or not? + +```java +"hexlet" +" hexlet" +``` + +## Terminology. String or line? + +In programming there is a terminological trap. + +- A string is a data type, that same set of characters in quotes, for example `"hello"` +- A line is a line of text in a file or in code + +For example, the code below has a line but no string. + +```java +System.out.println(5); +``` + +To avoid confusion, in this course we will stick to the following wording. + +- String, when we talk about the data type +- Line, when we talk about lines of code -Everything is clear with the first two, these are exactly strings, we have already worked with similar constructions and said that strings are character sets. +## Only double quotes -Any single character in quotes is a string. The empty string `" "` is also a string. That is, we consider everything to be inside the quotes, even if it is a space, one character or no characters at all. +In some languages, strings can be written in both single and double quotes. In Java, a string is always enclosed in double quotes `"`. -Imagine that you want to print _dragon's mother_ line. The apostrophe before the letter **s** is the same character as a single quote. Let's try: +```java +System.out.println("Hello"); +``` + +Single quotes also appear in Java, but that's a completely different story. A notation like `'A'` denotes a single character, not a string. So when it comes to text, we use only double quotes. -This version of the program will work correctly: +## The problem with quotes inside a string + +Imagine you want to print the phrase *Dragon's mother*. It contains an apostrophe (*'s*), which matches the single quote character. Since the string is bounded by double quotes, the apostrophe inside it doesn't cause any trouble. ```java System.out.println("Dragon's mother"); +// => Dragon's mother ``` -   -And what if we want to create such a string: + +Java understands that a single quote inside a string is an ordinary character. A string starts and ends with double quotes, and everything between them is considered its content. + +The difficulties begin when you need the double quotes themselves inside the string. Imagine we want to print the following text: ```text Dragon's mother said "No" ``` -It contains both single and double quotes. How to be in this situation? +If you insert double quotes directly inside the string, the program won't compile. + +```java +// Ends with a syntax error +System.out.println("Dragon's mother said "No""); +``` + +From Java's point of view, this results in a strange construction of three parts. + +- The string `"Dragon's mother said "` +- The word `No`, which is not in quotes and therefore is not considered a string +- The empty string `""` + +Java will decide that the first string ends before the word *No*, and then comes incomprehensible code. This is what causes the error. -To do this, use the **escape character**: `\`. If you put `\` before the quotation mark (single or double), it will mean that the quotation mark should be viewed not as the beginning or end of the line, but as part of the line. +## The escape character + +To place a double quote inside a string, you escape it with a backslash `\`. It tells the compiler that the character following it is part of the string, not the string's boundary. ```java System.out.println("Dragon's mother said \"No\""); +// => Dragon's mother said "No" +``` + +Here we escape the double quotes inside a string enclosed in double quotes. Java treats `\"` as a single quote character, not as two separate characters. Such notations are called escape sequences. + +To print the backslash itself, you also escape it with a second slash. + +```java +System.out.println("\\"); +// => \ ``` diff --git a/modules/25-strings/10-quotes/en/data.yml b/modules/25-strings/10-quotes/en/data.yml index 018b5ca..3c93301 100644 --- a/modules/25-strings/10-quotes/en/data.yml +++ b/modules/25-strings/10-quotes/en/data.yml @@ -1,2 +1,5 @@ --- -name: quotes +name: Quotes +tips: + - > + [Strings in Java](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/String.html) diff --git a/modules/25-strings/10-quotes/es/EXERCISE.md b/modules/25-strings/10-quotes/es/EXERCISE.md index 75da0da..32c221d 100644 --- a/modules/25-strings/10-quotes/es/EXERCISE.md +++ b/modules/25-strings/10-quotes/es/EXERCISE.md @@ -1,8 +1,8 @@ -Escribe un programa que imprima en la pantalla: +Escribe un programa que muestre en la pantalla: ```text "Khal Drogo's favorite word is "athjahakar"" ``` -El programa debe imprimir exactamente esta frase. Presta atención a las comillas al principio y al final de la frase. +El programa debe mostrar esta frase exactamente. Presta atención a las comillas al principio y al final de la frase. diff --git a/modules/25-strings/10-quotes/es/README.md b/modules/25-strings/10-quotes/es/README.md index 0f9767c..76ea2e7 100644 --- a/modules/25-strings/10-quotes/es/README.md +++ b/modules/25-strings/10-quotes/es/README.md @@ -1,44 +1,107 @@ +Las cadenas de texto en Java aparecen a cada paso. Con su ayuda trabajamos con texto, mostramos mensajes en la pantalla, procesamos la entrada del usuario e intercambiamos datos con sistemas externos. + +Desde el punto de vista de Java, una cadena de texto es un conjunto de caracteres encerrado entre comillas dobles. Veamos algunos ejemplos. + ```java -"Hola" -"Adiós" +"Hello" +"Goodbye" "G" " " "" ``` -¿Cuáles de estas cinco opciones son cadenas de texto? Con las primeras dos está claro: son definitivamente cadenas de texto, ya hemos trabajado con construcciones similares. ¿Pero qué pasa con las demás? +Todas estas variantes son cadenas de texto. + +- `"Hello"` y `"Goodbye"` son cadenas de varios caracteres +- `"G"` es una cadena de un solo carácter +- `" "` es una cadena de un solo espacio +- `""` es una cadena vacía, no contiene ningún carácter. Cumple el mismo papel que el 0 en matemáticas + +Todo lo que está dentro de las comillas se considera una cadena de texto, aunque solo haya un espacio o nada en absoluto. + +Si mostramos estas cadenas en la pantalla, `"Hello"` y `"Goodbye"` se verán con claridad. En cambio, `" "` y `""` resultan confusas. Mostrar una cadena vacía se ve como una ausencia total de texto, mientras que una cadena con un espacio muestra un "espacio vacío" que visualmente es difícil de distinguir del vacío. Java distingue con claridad estos casos. Una cadena vacía significa la ausencia de caracteres, mientras que una cadena con un espacio contiene un carácter de espacio concreto. + +Pregunta de control. ¿Son iguales estas cadenas o no? + +```java +"hexlet" +" hexlet" +``` + +## Terminología. ¿Cadena o línea? -Cualquier carácter individual entre comillas es una cadena de texto. Una cadena de texto vacía `""` también es una cadena de texto. Es decir, consideramos como cadena de texto todo lo que está dentro de las comillas, incluso si es un espacio, un solo carácter o la ausencia total de caracteres. +En programación existe una trampa terminológica. -Imagina que quieres imprimir la frase *la madre del dragón*. El apóstrofe antes de la letra **s** es un carácter igual que una comilla simple. Intentemos. Esta versión del programa funcionará correctamente: +- Una cadena (string) es un tipo de dato, ese mismo conjunto de caracteres entre comillas, por ejemplo `"hello"` +- Una línea (line) es una línea de texto en un archivo o en el código + +Por ejemplo, en el código de abajo hay una línea, pero no una cadena. ```java -System.out.println("La madre del dragón"); +System.out.println(5); ``` -Pero, ¿qué pasa si queremos crear una cadena de texto como esta: +Para no confundirnos, en este curso nos atendremos a las siguientes formulaciones. + +- Cadena, cuando hablamos del tipo de dato +- Línea, cuando hablamos de las líneas de código + +## Solo comillas dobles + +En algunos lenguajes las cadenas se pueden escribir tanto con comillas simples como con comillas dobles. En Java una cadena siempre se encierra entre comillas dobles `"`. + +```java +System.out.println("Hello"); +``` + +Las comillas simples también aparecen en Java, pero esa es una historia completamente distinta. Una notación como `'A'` representa un solo carácter, no una cadena. Por eso, cuando se trata de texto, usamos solo comillas dobles. + +## El problema con las comillas dentro de una cadena + +Imagina que quieres imprimir la frase *Dragon's mother*. En ella hay un apóstrofo (*'s*), que coincide con el carácter de comilla simple. Como la cadena está delimitada por comillas dobles, el apóstrofo que hay dentro no molesta. + +```java +System.out.println("Dragon's mother"); +// => Dragon's mother +``` + +Java entiende que una comilla simple dentro de una cadena es un carácter corriente. La cadena empieza y termina con comillas dobles, y todo lo que hay entre ellas se considera su contenido. + +Las dificultades empiezan cuando dentro de la cadena hacen falta las propias comillas dobles. Imagina que queremos mostrar el siguiente texto: ```text -La madre del dragón dijo "No" +Dragon's mother said "No" ``` -En ella hay tanto comillas simples como comillas dobles. ¿Qué hacer en esta situación? Si simplemente intentamos imprimir esta cadena de texto, obtendremos un error: +Si insertamos las comillas dobles directamente dentro de la cadena, el programa no compilará. ```java -// Terminará con un error de sintaxis -System.out.println("La madre del dragón dijo "No""); +// Termina con un error de sintaxis +System.out.println("Dragon's mother said "No""); ``` -Desde el punto de vista de Java, esta es una construcción extraña de dos componentes de tres: +Desde el punto de vista de Java, aquí resulta una construcción extraña de tres partes. + +- La cadena `"Dragon's mother said "` +- La palabra `No`, que no está entre comillas y por eso no se considera una cadena +- La cadena vacía `""` + +Java decidirá que la primera cadena termina antes de la palabra *No*, y a continuación viene código incomprensible. Eso es lo que provoca el error. + +## El carácter de escape -- La cadena de texto *"La madre del dragón dijo "* -- La cadena de texto *""* -- La palabra *No* entre ellos, que no se considera una cadena de texto porque no está entre comillas +Para colocar una comilla doble dentro de una cadena, se la escapa con una barra invertida `\`. Le indica al compilador que el carácter que le sigue es parte de la cadena, no el límite de la cadena. + +```java +System.out.println("Dragon's mother said \"No\""); +// => Dragon's mother said "No" +``` -No se puede imprimir esta cadena de texto de la manera habitual. Para imprimir cadenas de texto como esta, se utiliza el **carácter de escape**: `\`. +Aquí escapamos las comillas dobles dentro de una cadena encerrada entre comillas dobles. Java interpreta `\"` como un único carácter de comilla, no como dos caracteres separados. Estas notaciones se llaman secuencias de escape. -Si colocamos `\` antes de una comilla, significa que la comilla debe considerarse como parte de la cadena de texto, no como el inicio o el final de la misma: +Para mostrar la propia barra invertida, también se la escapa con una segunda barra. ```java -System.out.println("La madre del dragón dijo \"No\""); +System.out.println("\\"); +// => \ ``` diff --git a/modules/25-strings/10-quotes/es/data.yml b/modules/25-strings/10-quotes/es/data.yml index 615ace6..524e1fc 100644 --- a/modules/25-strings/10-quotes/es/data.yml +++ b/modules/25-strings/10-quotes/es/data.yml @@ -1,2 +1,5 @@ --- name: Comillas +tips: + - > + [Cadenas en Java](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/String.html) diff --git a/modules/25-strings/15-escape-characters/en/EXERCISE.md b/modules/25-strings/15-escape-characters/en/EXERCISE.md index 3d39a20..90e7138 100644 --- a/modules/25-strings/15-escape-characters/en/EXERCISE.md +++ b/modules/25-strings/15-escape-characters/en/EXERCISE.md @@ -1,7 +1,7 @@ Write a program that displays: - — Did Joffrey agree? - — He did. He also said "I love using \n". + - Did Joffrey agree? + - He did. He also said "I love using \n". -The program uses only one `println`, but the result on the screen should look exactly like the one shown above. +The program uses only one `System.out.println()`, but the result on the screen should look exactly as shown above. diff --git a/modules/25-strings/15-escape-characters/en/README.md b/modules/25-strings/15-escape-characters/en/README.md index 3eb7662..8352ecd 100644 --- a/modules/25-strings/15-escape-characters/en/README.md +++ b/modules/25-strings/15-escape-characters/en/README.md @@ -1,58 +1,74 @@ - -We want to show the dialogue of the Mother of Dragons with her child: +We want to show a dialogue between the Mother of Dragons and her child: ```text - Are you hungry? - Aaaarrrgh! ``` -If you display a line with the following text: +If you print a string with this text to the screen: ```java -System.out.print("— Are you hungry?— Aaaarrrgh!"); +System.out.println("- Are you hungry?- Aaaarrrgh!"); ``` -it will turn out like this: +you get this: ```text -— Are you hungry?— Aaaarrrgh! +- Are you hungry?- Aaaarrrgh! ``` -We need to somehow tell the interpreter "click on enter" — to make a line break after the question mark. - -In Java, `\n` is a line break: +Not what we wanted. The lines are placed one after another, not one below the other. We need to somehow tell the interpreter to "press Enter" — to make a line break after the question mark. This can be done using the line feed character: `\n`: ```java -System.out.print("— Are you hungry?\n— Aaaarrrgh!"); +System.out.println("- Are you hungry?\n- Aaaarrrgh!"); ``` result: ```text -— Are you hungry? -— Aaaarrrgh! +- Are you hungry? +- Aaaarrrgh! ``` -`\n` is an example **of the escape sequence** (escape sequence). They are also called control structures. +`\n` is a special character. In the literature it is often denoted as *LF* (Line Feed). You might think this is a typo, since here we see two characters `\` and `n`, but that's not the case. From the computer's point of view, this is a single invisible line feed character: + +```java +// We haven't studied this, but you should know the truth +// Below is code that returns the length of a string +"a".length(); // 1 +"\n".length(); // 1 !!! +"\n\n".length(); // 2 !!! +``` + +Why is it done this way? `\n` is just a way to write the line feed character, but the line feed itself, by its meaning, is a single character, though an invisible one. + +That is exactly why such a task arose. It had to be represented somehow on the keyboard. And since the number of keys on the keyboard is limited and reserved for the most important ones, all special characters are implemented as such notations. -While typing in some Word, you press Enter at the end of the line. The editor puts a special invisible character at the end of the line, which is called LINE FEED (LF). In some editors, you can even turn on the display of invisible characters. Then the text will look something like this: +The line feed character is not something specific to programming. Everyone who has ever typed on a computer has used a line feed by pressing Enter. + +Many editors have an option that lets you turn on the display of invisible characters. This option helps you understand where they are, although it is only a schematic display, since these invisible characters have no graphical representation: ```text -— Hello!¶ -— Oh, hi!¶ -— How are you? +- Hello!¶ +- Oh, hi!¶ +- How are you? ``` -A device that outputs the corresponding text takes this symbol into account. For example, the printer, when meeting with LF, pulls the paper up one line, and the text editor transfers all subsequent text below, also one line. +A device that outputs the corresponding text takes this character into account. For example, when a printer encounters LF, it pulls the paper up one line, and a text editor moves all subsequent text below, also by one line. + +`\n` is an example of an **escape sequence**. They are also called control constructs. Although there are dozens of such characters, in programming only a few are commonly encountered. + +Besides the line feed, such characters include: -Although there are more than a dozen of such characters, there are often only a few in programming. In addition to line breaks, these characters include tabulation (a break, obtained by pressing the Tab button) and carriage returnValue (only in Windows). We, programmers, often need to use, for example, the translation of the string `\n` to properly format the text. +* Tabulation — the break you get by pressing the Tab key +* Carriage return (only in Windows) + +Programmers often need to use the line feed `\n` to format text correctly: ```java -System.out.print("Gregor Clegane\nDunsen\nPolliver\nChiswyck"); +System.out.println("Gregor Clegane\nDunsen\nPolliver\nChiswyck"); ``` -**Attention! Escaping sequences like `\n` work only inside double quotes!** - The screen will display: ```text @@ -62,51 +78,65 @@ Polliver Chiswyck ``` -For convenience, there is a method `System.out.println`, which allows you to display some value on the console and then transfer the console to the next line. For example: +Pay attention to the following points: -```java -System.out.println("Hello"); -System.out.println("World"); -``` +1. It does not matter what comes before or after `\n`: a character or an empty string. The line feed will be detected and performed in any case -The screen will display: +2. Remember that a string can contain one character or no characters at all. And a string can also contain only `\n`. Analyze the following example: -```text -Hello -World -``` + ```java + System.out.println("\n"); + System.out.println("Dunsen"); + ``` -Pay attention to the following points: + Here we first print the "line feed" string, and then print an ordinary string. The program will display: -1\. It does not matter what comes before or after `\n`: a character or an empty string. The transfer will be detected and executed in any case. + + ```text + ㅤ + ㅤ + Dunsen + ``` -2\. Remember that a string can contain only one character or zero characters at all? And the line can contain only `\n`: + Why did two empty lines appear before the string *Dunsen*, and not one? The point is that `System.out.println()` automatically adds a line feed character to the end when it outputs a value. -```java -System.out.print("Gregor Clegane"); -System.out.print("\n"); -System.out.print("Dunsen"); -``` + Thus, we specified one line feed explicitly, passing this escape sequence character as an argument to the function, and the second line feed was added by the function itself automatically. -Here we print one line with the name, then one line “line feed”, and then another line. The program will display: + Another code example: -```text -Gregor Clegane -Dunsen -``` + ```java + System.out.println("Polliver"); + System.out.println("Gregor Clegane"); + System.out.println(); + System.out.println("Chiswyck\n"); + System.out.println("Dunsen"); + ``` -3\. Despite the fact that in the source code of a program, a sequence of type `\n` looks like two characters, from the point of view of the interpreter it is a special single character. + The output will be like this: -4\. If we need to display `\n` exactly as text (two separate printable characters), then we can use the already known screening method, adding one more `\` at the beginning. That is, the sequence `\\n` is displayed as the characters `\` and `n` following each other. + ```text + Polliver + Gregor Clegane -```java -System.out.print("Joffrey loves using \\n"); -``` + Chiswyck -the screen will be released: + Dunsen + ``` -```text -Joffrey loves using \n -``` + You now have enough knowledge to figure out on your own why the output was formed exactly this way. + +3. If we need to display `\n` exactly as text (two separate printable characters), we can use the escaping method we already know, adding one more `\` at the beginning. That is, the sequence `\\n` will be displayed as the characters `\` and `n` following one after another: + + ```java + System.out.println("Joffrey loves using \\n"); + ``` + + the screen will display: + + ```text + Joffrey loves using \n + ``` + +A small but important note about Windows. In Windows, `\r\n` is used for line breaks by default — this is due to historical reasons. Such a combination works well only in Windows, but creates problems when transferring to other systems: for example, when a development team has both Windows and Linux users. -A small but important note about Windows. On Windows, `\r\n` is used to translate strings by default. Such a combination works well only in Windows, but creates problems when transferring to other systems (for example, when the development team has both Windows and Linux users). The fact is that the sequence `\r\n` has a different interpretation depending on the selected encoding (discussed later). For this reason, in the development environment, it is customary to always use `\n` without `\r`, since LF is always treated the same way and works fine in any system. Remember to configure your editor to use `\n`. +The point is that the sequence `\r\n` is interpreted differently depending on the chosen encoding. That is why in the developer community it is customary to always use `\n` without `\r`, since LF is always interpreted the same way and works perfectly in any system. Don't forget to configure your editor to use `\n`. diff --git a/modules/25-strings/15-escape-characters/en/data.yml b/modules/25-strings/15-escape-characters/en/data.yml index 3272f58..c4974bb 100644 --- a/modules/25-strings/15-escape-characters/en/data.yml +++ b/modules/25-strings/15-escape-characters/en/data.yml @@ -1,5 +1,6 @@ --- -name: Escaping sequences +name: Escape sequences tips: - - | - [String History](https://en.wikipedia.org/wiki/Newline#History)j + - > + [History of the + newline](https://en.wikipedia.org/wiki/Newline#History) diff --git a/modules/25-strings/20-strings-concatenation/en/EXERCISE.md b/modules/25-strings/20-strings-concatenation/en/EXERCISE.md index 2051eb3..1f56bfc 100644 --- a/modules/25-strings/20-strings-concatenation/en/EXERCISE.md +++ b/modules/25-strings/20-strings-concatenation/en/EXERCISE.md @@ -1,8 +1,8 @@ -Display +Using `System.out.println()`, concatenate the words and display the phrase on the screen: ```text Winter came for the House of Frey. ``` -using concatenation of words. Each word must be represented in a separate line. Do not forget about the spaces. +Each word must be written separately and enclosed in quotes, for example "Winter ". Don't forget the spaces after each word. diff --git a/modules/25-strings/20-strings-concatenation/en/README.md b/modules/25-strings/20-strings-concatenation/en/README.md index 8135a21..8b5e63c 100644 --- a/modules/25-strings/20-strings-concatenation/en/README.md +++ b/modules/25-strings/20-strings-concatenation/en/README.md @@ -1,48 +1,61 @@ - -We already know about the mathematical operation of addition. Such a program: +Programs constantly work with strings. Everything we see on websites or in mobile apps is in one way or another represented as text. This text is most often **dynamic** — assembled from different parts that are joined together. For example, we combine a first name and a last name, add a unit of measurement, or build a message from a template. The operation of joining strings in programming is called **concatenation** and looks like this: ```java -System.out.print(5 + 3); +// The operator is the same as for adding numbers, +// but here it has a different meaning (semantics) +System.out.println("Dragon" + "stone"); +// => Dragonstone ``` -will display `8` - the result of the binary operator `+` with the operands `5` and `3`. - -Above the strings defined their operations. You can "fold" two lines. Such a program: +Strings are always glued together in the same order in which the operands are written. The left operand becomes the left part of the result, and the right one becomes the right part. You can concatenate as many parts in a row as you like. Here are a few more examples: ```java -System.out.print("Dragon" + "stone"); +System.out.println("Kings" + "wood"); // => Kingswood +// Reverse word order +System.out.println("road" + "Kings"); // => roadKings +// You can concatenate absolutely any strings +System.out.println("King's" + "Landing"); // => King'sLanding ``` -will display `Dragonstone` - the result of the binary operator `+` with the 'Dragon' and 'stone' operands. +In the last example, the name of the city came out with a mistake: *King's Landing* should be written with a space. But there were no spaces in our original strings, and spaces in the code itself around the `+` symbol don't affect anything, because they are not part of the strings. -This operation is called **concatenation**. Roughly speaking, this is the "gluing" of the lines. Gluing always happens in the same order in which the operands are written, in other words, the left operand becomes the left part of the string, and the right operand becomes the left. +## A space is also a character -Here are some more examples: +When joining, Java does not insert spaces automatically. If you need a space between the parts, you add it manually. There are three ways out of the *King's Landing* situation: ```java -System.out.print("Kings" + "wood"); // => Kingswood - -System.out.print("Kings" + "road"); // => Kingsroad - -System.out.print("King's" + "Landing"); // => King'sLanding +// Put a space in the left part +System.out.println("King's " + "Landing"); // => King's Landing +// Put a space in the right part +System.out.println("King's" + " Landing"); // => King's Landing +// Add a space separately +System.out.println("King's" + " " + "Landing"); // => King's Landing ``` -In the last example, the name of the city turned out to be with an error: *King's Landing* must be written with a space! But there were no spaces in our initial lines, and the spaces in the code to the left and right of the `+` character do not matter, because they are not part of the lines. +The result is the same in all three cases. A space is the same kind of character as the others, and the more spaces there are in the strings, the wider the gap between the words. + +## Concatenation with a number -Let's try to solve this problem in different ways: +The `+` operator can glue a string not only with a string, but also with a number. In this case the number turns into text and is stuck onto the string: ```java -System.out.print("King's " + "Landing"); // => King's Landing +System.out.println("Sum: " + 5); +// => Sum: 5 +``` -System.out.print("King's" + " Landing"); // => King's Landing +On the left is a string, on the right is the number `5`. Java prints them together, as a single piece of text. -System.out.print("King's" + " " + "Landing"); // => King's Landing -``` +## Escape sequences -A space is the same character as the others, therefore, how many spaces to put in a line will be as much: +Strings can contain escape sequences, for example `\n` for a line feed. During concatenation they work the same way as any other characters: ```java -System.out.print("King's " + " Landing"); // => King's Landing - -System.out.print("King's " + " Landing"); // => King's Landing +System.out.println("Hello," + "\n" + "World!"); +// => +// Hello, +// World! ``` + +## Conclusion + +Concatenation is the joining of strings using `+`. The gluing goes strictly from left to right, in the order of the operands. Spaces are not added automatically, so they are included in the strings manually. diff --git a/modules/25-strings/20-strings-concatenation/en/data.yml b/modules/25-strings/20-strings-concatenation/en/data.yml index c4df54d..ee59369 100644 --- a/modules/25-strings/20-strings-concatenation/en/data.yml +++ b/modules/25-strings/20-strings-concatenation/en/data.yml @@ -1,2 +1,10 @@ --- name: Concatenation +tips: + - > + If the editor has `// BEGIN` and `// END` markers, then you should write your + code between these lines. +definitions: + - name: Concatenation + description: | + the operation of joining two strings. For example, `System.out.println("King's " + "Landing")` diff --git a/modules/25-strings/20-strings-concatenation/es/EXERCISE.md b/modules/25-strings/20-strings-concatenation/es/EXERCISE.md index ff54e06..4661851 100644 --- a/modules/25-strings/20-strings-concatenation/es/EXERCISE.md +++ b/modules/25-strings/20-strings-concatenation/es/EXERCISE.md @@ -1,8 +1,8 @@ -Utilizando `System.out.println()`, realiza la concatenación de palabras y muestra en la pantalla la siguiente frase: +Utilizando `System.out.println()`, realiza la concatenación de las palabras y muestra en la pantalla la frase: ```text Winter came for the House of Frey. ``` -Cada palabra debe ser escrita por separado y entre comillas, por ejemplo "Winter ". No olvides los espacios después de cada palabra. +Cada palabra debe escribirse por separado y entre comillas, por ejemplo "Winter ". No olvides los espacios después de cada palabra. diff --git a/modules/25-strings/20-strings-concatenation/es/README.md b/modules/25-strings/20-strings-concatenation/es/README.md index e7dc00c..9cd76f3 100644 --- a/modules/25-strings/20-strings-concatenation/es/README.md +++ b/modules/25-strings/20-strings-concatenation/es/README.md @@ -1,38 +1,61 @@ -Los programas constantemente operan con cadenas de texto. Todo lo que vemos en sitios web o aplicaciones móviles está representado de alguna manera como texto. Este texto, en su mayoría, es **dinámico**, es decir, se obtiene a partir de diferentes partes que se unen entre sí. La operación de unir cadenas de texto en programación se llama **concatenación** y se ve así: +Los programas operan con cadenas de texto constantemente. Todo lo que vemos en los sitios web o en las aplicaciones móviles está representado de una u otra forma como texto. Ese texto casi siempre es **dinámico**: se ensambla a partir de distintas partes que se unen entre sí. Por ejemplo, unimos el nombre y el apellido, agregamos una unidad de medida o componemos un mensaje a partir de una plantilla. La operación de unir cadenas de texto en programación se llama **concatenación** y se ve así: ```java -// El operador es el mismo que se utiliza para sumar números, +// El operador es el mismo que para sumar números, // pero aquí tiene un significado diferente (semántica) System.out.println("Dragon" + "stone"); // => Dragonstone ``` -La concatenación de cadenas siempre ocurre en el mismo orden en el que se escriben los operandos. El operando izquierdo se convierte en la parte izquierda de la cadena, y el operando derecho se convierte en la parte derecha. Aquí hay algunos ejemplos más: +La concatenación de cadenas siempre ocurre en el mismo orden en el que se escriben los operandos. El operando izquierdo se convierte en la parte izquierda del resultado, y el derecho, en la parte derecha. Se pueden concatenar tantas partes seguidas como se quiera. Aquí hay algunos ejemplos más: ```java System.out.println("Kings" + "wood"); // => Kingswood // Orden inverso de las palabras System.out.println("road" + "Kings"); // => roadKings -// Se pueden concatenar cualquier tipo de cadenas +// Se puede concatenar absolutamente cualquier cadena System.out.println("King's" + "Landing"); // => King'sLanding ``` -En el último ejemplo, el nombre de la ciudad se escribió incorrectamente: *King's Landing* debería tener un espacio en medio. Pero en nuestras cadenas iniciales no había espacios, y los espacios alrededor del símbolo `+` en el código no tienen importancia, ya que no forman parte de las cadenas. +En el último ejemplo, el nombre de la ciudad quedó con un error: *King's Landing* debe escribirse con un espacio. Pero en nuestras cadenas originales no había espacios, y los espacios que hay en el propio código alrededor del símbolo `+` no influyen en nada, porque no forman parte de las cadenas. -Hay tres formas de solucionar esta situación: +## El espacio también es un carácter + +Al unir cadenas, Java no inserta espacios automáticamente. Si hace falta un espacio entre las partes, se agrega manualmente. Hay tres salidas para la situación de *King's Landing*: ```java -// Agregar un espacio en la parte izquierda -System.out.println("King's " + "Landing"); // => King's Landing -// Agregar un espacio en la parte derecha -System.out.println("King's" + " Landing"); // => King's Landing -// Agregar un espacio por separado -System.out.println("King's" + " " + "Landing"); // => King's Landing +// Ponemos un espacio en la parte izquierda +System.out.println("King's " + "Landing"); // => King's Landing +// Ponemos un espacio en la parte derecha +System.out.println("King's" + " Landing"); // => King's Landing +// Agregamos un espacio por separado +System.out.println("King's" + " " + "Landing"); // => King's Landing ``` -Un espacio es simplemente un símbolo más, al igual que los demás. Cuantos más espacios haya, más grande será la separación: +El resultado es el mismo en los tres casos. El espacio es un carácter como cualquier otro, y cuantos más espacios haya en las cadenas, más ancha será la separación entre las palabras. + +## Concatenación con un número + +El operador `+` puede unir una cadena no solo con otra cadena, sino también con un número. En ese caso el número se convierte en texto y se pega a la cadena: ```java -System.out.println("King's " + " Landing"); // => King's Landing -System.out.println("King's " + " Landing"); // => King's Landing +System.out.println("Suma: " + 5); +// => Suma: 5 ``` + +A la izquierda hay una cadena, a la derecha el número `5`. Java los muestra juntos, como un único texto. + +## Secuencias de escape + +En las cadenas aparecen secuencias de escape, por ejemplo `\n` para el salto de línea. Durante la concatenación funcionan igual que cualquier otro carácter: + +```java +System.out.println("Hello," + "\n" + "World!"); +// => +// Hello, +// World! +``` + +## Conclusión + +La concatenación es la unión de cadenas mediante `+`. La unión va estrictamente de izquierda a derecha, en el orden de los operandos. Los espacios no se agregan automáticamente, por eso se incluyen en las cadenas manualmente. diff --git a/modules/25-strings/20-strings-concatenation/es/data.yml b/modules/25-strings/20-strings-concatenation/es/data.yml index 57a6ba2..4ee8d4a 100644 --- a/modules/25-strings/20-strings-concatenation/es/data.yml +++ b/modules/25-strings/20-strings-concatenation/es/data.yml @@ -1,2 +1,10 @@ --- name: Concatenación +tips: + - > + Si en el editor aparecen las marcas `// BEGIN` y `// END`, debes escribir el + código entre esas líneas. +definitions: + - name: Concatenación + description: | + operación de unir dos cadenas de texto. Por ejemplo, `System.out.println("King's " + "Landing")` diff --git a/modules/25-strings/30-multiline-strings/App.java b/modules/25-strings/30-multiline-strings/App.java index d5e1dd2..f7177a7 100644 --- a/modules/25-strings/30-multiline-strings/App.java +++ b/modules/25-strings/30-multiline-strings/App.java @@ -2,8 +2,8 @@ public class App { public static void main(String[] args) { // BEGIN System.out.println(""" - Лето - пришло"""); + Summer + has come"""); // END } } diff --git a/modules/25-strings/30-multiline-strings/AppTest.java b/modules/25-strings/30-multiline-strings/AppTest.java index 7d63b0c..c7b93ab 100644 --- a/modules/25-strings/30-multiline-strings/AppTest.java +++ b/modules/25-strings/30-multiline-strings/AppTest.java @@ -7,7 +7,7 @@ class AppTest { public static void main(String[] args) { - final var expected = "Лето\nпришло"; + final var expected = "Summer\nhas come"; ByteArrayOutputStream out = new ByteArrayOutputStream(); System.setOut(new PrintStream(out)); diff --git a/modules/25-strings/30-multiline-strings/en/EXERCISE.md b/modules/25-strings/30-multiline-strings/en/EXERCISE.md new file mode 100644 index 0000000..8f4e0b2 --- /dev/null +++ b/modules/25-strings/30-multiline-strings/en/EXERCISE.md @@ -0,0 +1,8 @@ +Using a text block `"""..."""`, print text of two lines on the screen: + +```text +Summer +has come +``` + +Adjust the indentation inside the block so that the words start from the first character, with no extra spaces on the left. diff --git a/modules/25-strings/30-multiline-strings/en/README.md b/modules/25-strings/30-multiline-strings/en/README.md new file mode 100644 index 0000000..9a0966c --- /dev/null +++ b/modules/25-strings/30-multiline-strings/en/README.md @@ -0,0 +1,67 @@ +Sometimes text in a program needs to span several lines. This happens when generating an email, creating a template, formatting an error message, or working with long texts. + +We've already seen one way. It's the newline character `\n` inside a regular string in double quotes: + +```java +System.out.println("Example text,\nconsisting of\nseveral lines"); +``` + +When printed, this is what you get: + +```text +Example text, +consisting of +several lines +``` + +This way works, but it's inconvenient. Each `\n` has to be inserted by hand, and the longer the text, the harder the code is to read. + +## Text blocks + +Java has a notation in which text in the code looks the same as when it's printed. This is a text block. It starts and ends with three double quotes `"""`. + +The opening `"""` is placed at the end of the line, and the text itself is written on the next line: + +```java +System.out.println(""" + Example text, + consisting of + several lines"""); +``` + +The output will be the same as the version with `\n`: + +```text +Example text, +consisting of +several lines +``` + +Java adds the line breaks inside the block itself, so you don't need to write `\n`. + +## Indentation inside a block + +It's convenient to shift the block's text to the right so it doesn't blend with the code. Java looks at the common indentation of all the block's lines and strips it from each line. In the example above, each line has four spaces on the left, and all four are removed from the result. In the output, the lines start from the first character. + +The position of the closing `"""` also affects the indentation. If you shift it to the left of the text, some of the spaces on the left will remain in the result. That's why the closing quotes are usually kept at the level of the text or right after the last line. + +## Quotes without escaping + +Inside a text block, double quotes can be written directly, without a backslash: + +```java +System.out.println(""" + Here "double" quotes are shown as is"""); +``` + +Output: + +```text +Here "double" quotes are shown as is +``` + +In a regular string, you would have to escape each quote with `\"`. + +## Conclusion + +A text block is written between triple double quotes `"""`. The opening `"""` is placed at the end of the line, and the text is written below. Java adds the line breaks itself, and the common indentation on the left is stripped. For the machine, a text block and a string with `\n` mean the same thing, but for a person a block is clearer. diff --git a/modules/25-strings/30-multiline-strings/en/data.yml b/modules/25-strings/30-multiline-strings/en/data.yml new file mode 100644 index 0000000..8201191 --- /dev/null +++ b/modules/25-strings/30-multiline-strings/en/data.yml @@ -0,0 +1,11 @@ +--- +name: Multiline strings +definitions: + - name: Text block + description: >- + a string literal of several lines, written between triple double + quotes `"""`. +tips: + - > + [Text + blocks](https://docs.oracle.com/en/java/javase/21/text-blocks/index.html) diff --git a/modules/25-strings/30-multiline-strings/es/EXERCISE.md b/modules/25-strings/30-multiline-strings/es/EXERCISE.md new file mode 100644 index 0000000..83bc125 --- /dev/null +++ b/modules/25-strings/30-multiline-strings/es/EXERCISE.md @@ -0,0 +1,8 @@ +Usando un bloque de texto `"""..."""`, muestra en la pantalla un texto de dos líneas: + +```text +Summer +has come +``` + +Ajusta la sangría dentro del bloque para que las palabras empiecen desde el primer carácter, sin espacios de más a la izquierda. diff --git a/modules/25-strings/30-multiline-strings/es/README.md b/modules/25-strings/30-multiline-strings/es/README.md new file mode 100644 index 0000000..5ef294d --- /dev/null +++ b/modules/25-strings/30-multiline-strings/es/README.md @@ -0,0 +1,67 @@ +A veces el texto en un programa debe ocupar varias líneas. Esto ocurre al generar una carta, crear una plantilla, formatear un mensaje de error o trabajar con textos largos. + +Ya hemos visto una forma. Es el carácter de salto de línea `\n` dentro de una cadena normal entre comillas dobles: + +```java +System.out.println("Texto de ejemplo,\nque consta de\nvarias líneas"); +``` + +Al imprimirlo, se obtiene esto: + +```text +Texto de ejemplo, +que consta de +varias líneas +``` + +Es una forma que funciona, pero incómoda. Cada `\n` hay que insertarlo a mano, y cuanto más largo es el texto, peor se lee el código. + +## Bloques de texto + +En Java existe una notación en la que el texto en el código se ve igual que al imprimirlo. Es un bloque de texto (text block). Comienza y termina con tres comillas dobles `"""`. + +Las `"""` de apertura se colocan al final de la línea, y el texto se escribe en la línea siguiente: + +```java +System.out.println(""" + Texto de ejemplo, + que consta de + varias líneas"""); +``` + +La salida será la misma que en la versión con `\n`: + +```text +Texto de ejemplo, +que consta de +varias líneas +``` + +Java añade los saltos de línea dentro del bloque por sí mismo, así que no hace falta escribir `\n`. + +## Sangría dentro del bloque + +Conviene desplazar el texto del bloque hacia la derecha para que no se confunda con el código. Java mira la sangría común de todas las líneas del bloque y la recorta de cada línea. En el ejemplo anterior, cada línea tiene cuatro espacios a la izquierda, y los cuatro desaparecen del resultado. En la salida, las líneas empiezan desde el primer carácter. + +La posición de las `"""` de cierre también afecta a la sangría. Si las desplazas a la izquierda del texto, parte de los espacios de la izquierda quedará en el resultado. Por eso las comillas de cierre suelen mantenerse al nivel del texto o justo después de la última línea. + +## Comillas sin escapar + +Dentro de un bloque de texto, las comillas dobles se pueden escribir directamente, sin barra invertida: + +```java +System.out.println(""" + Aquí las comillas "dobles" se ven tal cual"""); +``` + +Salida: + +```text +Aquí las comillas "dobles" se ven tal cual +``` + +En una cadena normal, para esto habría que escapar cada comilla con `\"`. + +## Conclusión + +Un bloque de texto se escribe entre tres comillas dobles `"""`. Las `"""` de apertura se colocan al final de la línea, y el texto se escribe debajo. Java añade los saltos de línea por sí mismo, y la sangría común de la izquierda se recorta. Para la máquina, un bloque de texto y una cadena con `\n` significan lo mismo, pero para una persona el bloque es más claro. diff --git a/modules/25-strings/30-multiline-strings/es/data.yml b/modules/25-strings/30-multiline-strings/es/data.yml new file mode 100644 index 0000000..628cf06 --- /dev/null +++ b/modules/25-strings/30-multiline-strings/es/data.yml @@ -0,0 +1,10 @@ +--- +name: Cadenas multilínea +definitions: + - name: Bloque de texto + description: >- + literal de cadena de varias líneas, escrito entre tres comillas dobles `"""`. +tips: + - > + [Bloques de + texto](https://docs.oracle.com/en/java/javase/21/text-blocks/index.html) diff --git a/modules/25-strings/30-multiline-strings/ru/EXERCISE.md b/modules/25-strings/30-multiline-strings/ru/EXERCISE.md index 9c5a09c..eab3b1d 100644 --- a/modules/25-strings/30-multiline-strings/ru/EXERCISE.md +++ b/modules/25-strings/30-multiline-strings/ru/EXERCISE.md @@ -2,8 +2,8 @@ Используя текстовый блок `"""..."""`, выведите на экран текст из двух строк: ```text -Лето -пришло +Summer +has come ``` Подберите отступ внутри блока так, чтобы слова начинались с первого символа без лишних пробелов слева. diff --git a/modules/25-strings/40-encoding/en/EXERCISE.md b/modules/25-strings/40-encoding/en/EXERCISE.md new file mode 100644 index 0000000..1791b1c --- /dev/null +++ b/modules/25-strings/40-encoding/en/EXERCISE.md @@ -0,0 +1,8 @@ + +Using `System.out.println()`, display a string with the copyright sign on the screen: + +```text +© 2024 Hexlet +``` + +The `©` character is not part of ASCII, but Java stores text in Unicode, so it is displayed without any additional settings. diff --git a/modules/25-strings/40-encoding/en/README.md b/modules/25-strings/40-encoding/en/README.md new file mode 100644 index 0000000..b47c069 --- /dev/null +++ b/modules/25-strings/40-encoding/en/README.md @@ -0,0 +1,59 @@ +At the very lowest level, a computer works only with zeros and ones. This is binary code. Each zero or one is called a bit (from binary digit). + +Any data inside a computer is a sequence of bits. This is how images, music, and text are stored. The usual numbers from the decimal system can also be represented in binary form: + +- 0 → `0` +- 1 → `1` +- 2 → `10` + +## How to encode text + +A computer does not "understand" letters. To store text, characters are turned into numbers. This is what encodings do. An encoding is a table where each character corresponds to a specific number. + +The most direct way is to number the letters, starting from one: + +- `a` → `1` +- `b` → `2` +- ...and so on up to `z` → `26` + +Now the word hello turns into a set of numbers: + +```text +h e l l o +↓ ↓ ↓ ↓ ↓ +8 5 12 12 15 +``` + +The program does not know that it is dealing with a word. It sees the instruction "show the character with code 8, then with code 5" and so on. The word good in the same table will turn into the codes 7, 15, 15, and 4. + +## ASCII. The first mass encoding + +The first computers worked mostly with the English language. For it, in the 1960s, the ASCII table of 128 characters was invented. It included the Latin alphabet, digits, punctuation marks, special characters like `@`, `#`, `!`, and control codes. + +For early programs this was enough, but not for the whole world. When computers reached other countries, a problem arose. ASCII has no Cyrillic, hieroglyphs, Arabic script, accents, or currency symbols. + +Then each country or company began to make its own table based on ASCII. Windows invented Windows-1251 for Russian, Apple created Mac Roman, and their own variants appeared in Asia and the Middle East. + +These tables were incompatible with each other. Code 226 in one encoding meant the letter é, and in another a completely different character. Text written in one table and read in another turned into garbage. + +## Garbled text + +If, instead of words, something like this appears in the text: + +```text +ÐÑÐ¸Ð²ÐµÑ +``` + +it means that the program read the bytes with the wrong table. It got a set of numbers but matched them with the wrong characters. In the 1990s and 2000s this happened all the time. + +## Unicode and UTF-8 + +To reduce all the tables to one, work on Unicode began in the 1990s. It is a common table that includes the characters of all the writing systems in the world. It has the Latin and Cyrillic alphabets, Chinese and Arabic script, mathematical signs, ancient alphabets, and even emoji. + +Inside Unicode there are several storage formats. The most common of them is UTF-8. It encodes English characters compactly, and expands as needed for the rest of the characters. + +Today UTF-8 is the default on the internet, in Linux, in databases, and in code editors. Java stores text in Unicode, so letters of any language and symbols like `©` or `€` are displayed the same way in any system. + +## Why you need to know this + +Text appears in every program, and encoding errors still happen. Most often this occurs when reading files, processing data, and exchanging data with databases. If garbled text appears on the screen, it is almost always a mixed-up encoding. It is worth remembering UTF-8 when working with external files too, because the editor or the system could have saved them in a different table. diff --git a/modules/25-strings/40-encoding/en/data.yml b/modules/25-strings/40-encoding/en/data.yml new file mode 100644 index 0000000..e0c10ca --- /dev/null +++ b/modules/25-strings/40-encoding/en/data.yml @@ -0,0 +1,12 @@ +--- +name: Encodings +definitions: + - name: Encoding + description: >- + a set of characters encoded using numbers to represent text in electronic + form. +tips: + - | + [ASCII code table](https://www.ascii-code.com/) + - | + [What are encodings?](https://ru.hexlet.io/blog/posts/encoding) diff --git a/modules/25-strings/40-encoding/es/EXERCISE.md b/modules/25-strings/40-encoding/es/EXERCISE.md new file mode 100644 index 0000000..81d6626 --- /dev/null +++ b/modules/25-strings/40-encoding/es/EXERCISE.md @@ -0,0 +1,8 @@ + +Utilizando `System.out.println()`, muestra en la pantalla una cadena con el signo de derechos de autor: + +```text +© 2024 Hexlet +``` + +El carácter `©` no forma parte de ASCII, pero Java almacena el texto en Unicode, por eso se muestra sin ajustes adicionales. diff --git a/modules/25-strings/40-encoding/es/README.md b/modules/25-strings/40-encoding/es/README.md new file mode 100644 index 0000000..2d309c5 --- /dev/null +++ b/modules/25-strings/40-encoding/es/README.md @@ -0,0 +1,59 @@ +En el nivel más bajo, la computadora trabaja solo con ceros y unos. Esto es el código binario. Cada cero o uno se llama bit (de binary digit, "dígito binario"). + +Cualquier dato dentro de la computadora es una secuencia de bits. Así se almacenan las imágenes, la música y el texto. Los números habituales del sistema decimal también se pueden representar en forma binaria: + +- 0 → `0` +- 1 → `1` +- 2 → `10` + +## Cómo codificar texto + +La computadora no "entiende" las letras. Para almacenar texto, los caracteres se convierten en números. De eso se encargan las codificaciones. Una codificación es una tabla donde a cada carácter le corresponde un número determinado. + +La forma más directa consiste en numerar las letras empezando por el uno: + +- `a` → `1` +- `b` → `2` +- ...y así hasta `z` → `26` + +Ahora la palabra hello se convierte en un conjunto de números: + +```text +h e l l o +↓ ↓ ↓ ↓ ↓ +8 5 12 12 15 +``` + +El programa no sabe que ante él hay una palabra. Ve la instrucción "muestra el carácter con el código 8, después con el código 5", y así sucesivamente. La palabra good en la misma tabla se convertirá en los códigos 7, 15, 15 y 4. + +## ASCII. La primera codificación masiva + +Las primeras computadoras trabajaban principalmente con el idioma inglés. Para él, en los años 1960 se ideó la tabla ASCII de 128 caracteres. En ella entraron el alfabeto latino, las cifras, los signos de puntuación, caracteres especiales como `@`, `#`, `!` y los códigos de control. + +Para los primeros programas esto bastaba, pero no para todo el mundo. Cuando las computadoras llegaron a otros países, surgió un problema. En ASCII no hay cirílico, ni jeroglíficos, ni escritura árabe, ni acentos, ni símbolos de moneda. + +Entonces cada país o empresa empezó a hacer su propia tabla basada en ASCII. Windows ideó Windows-1251 para el ruso, Apple creó Mac Roman, y en Asia y en Oriente Medio aparecieron sus propias variantes. + +Estas tablas eran incompatibles entre sí. El código 226 en una codificación significaba la letra é, y en otra un carácter completamente distinto. El texto escrito con una tabla y leído con otra se convertía en basura. + +## Caracteres ilegibles + +Si en el texto, en lugar de palabras, aparece algo así: + +```text +ÐÑÐ¸Ð²ÐµÑ +``` + +significa que el programa leyó los bytes con la tabla equivocada. Obtuvo un conjunto de números, pero los asoció con caracteres incorrectos. En los años 1990 y 2000 esto ocurría constantemente. + +## Unicode y UTF-8 + +Para reducir todas las tablas a una sola, en los años 1990 se empezó a crear Unicode. Es una tabla común en la que entraron los caracteres de todos los sistemas de escritura del mundo. En ella hay alfabeto latino y cirílico, escritura china y árabe, signos matemáticos, alfabetos antiguos e incluso emojis. + +Dentro de Unicode hay varios formatos de almacenamiento. El más extendido de ellos es UTF-8. Codifica los caracteres ingleses de forma compacta, y para el resto de los caracteres se expande según haga falta. + +Hoy UTF-8 está por defecto en internet, en Linux, en las bases de datos y en los editores de código. Java almacena el texto en Unicode, por eso las letras de cualquier idioma y símbolos como `©` o `€` se muestran igual en cualquier sistema. + +## Para qué saber esto + +El texto aparece en cada programa, y los errores de codificación siguen ocurriendo. Con más frecuencia esto sucede al leer archivos, procesar datos e intercambiar datos con las bases de datos. Si en la pantalla aparecen caracteres ilegibles, casi siempre es una codificación confundida. Conviene recordar UTF-8 también al trabajar con archivos externos, ya que el editor o el sistema podrían haberlos guardado con otra tabla. diff --git a/modules/25-strings/40-encoding/es/data.yml b/modules/25-strings/40-encoding/es/data.yml new file mode 100644 index 0000000..148e055 --- /dev/null +++ b/modules/25-strings/40-encoding/es/data.yml @@ -0,0 +1,12 @@ +--- +name: Codificaciones +definitions: + - name: Codificación + description: >- + conjunto de caracteres codificados mediante números para representar texto + en formato electrónico. +tips: + - | + [Tabla de códigos ASCII](https://www.ascii-code.com/) + - | + [¿Qué son las codificaciones?](https://es.wikipedia.org/wiki/Codificación_de_caracteres) diff --git a/modules/25-strings/description.en.yml b/modules/25-strings/description.en.yml index 9fa187a..b690e16 100644 --- a/modules/25-strings/description.en.yml +++ b/modules/25-strings/description.en.yml @@ -2,4 +2,4 @@ name: Strings description: | - The text in programming is called "strings", and this topic is not as simple as it may seem. Escaping, control characters and other aspects often start up novice developers. + Text in programming is called "strings", and this topic is not as simple as it may seem. How do you print a phrase that contains both single and double quotes? And how do you deal with text at all, since the computer knows nothing about letters! This module is devoted to different aspects of writing text — from quotes and escaping to encoding.