Strings · 字符串
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| string/strɪŋ/ | 字符串 | zì fú chuàn |
| ordered sequence/ˈɔːdəd ˈsiːkwəns/ | 有序序列 | yǒu xù xù liè |
| characters/ˈkærɪktəz/ | 字符 | zì fú |
| Concatenation/kənˌkætəˈneɪʃn/ | 拼接 | pīn jiē |
| Length/leŋθ/ | 长度 | cháng dù |
| substring/ˈsʌbstrɪŋ/ | 子串 | zi chuàn |
| user input/ˈjuːzə ˈɪnpʊt/ | 用户输入 | yòng hù shū rù |
Text is a string
- Text in a program is stored as a string 字符串.
- A string is an ordered sequence 有序序列 of characters 字符.
- "Hello" is a string of five characters.
- Order matters, so "abc" and "cba" are different strings.
文本是字符串
- 程序中的文本存储为一个字符串(string)。
- 字符串是一个有序序列(ordered sequence)的字符(characters)。
- "Hello" 是一个五个字符的字符串。
- 顺序重要,所以 "abc" 和 "cba" 是不同的字符串。
A string is: · 一个字符串是:
Order matters, so "abc" ≠ "cba". · 顺序重要,所以 "abc" ≠ "cba"。
Concatenating "sun" and "flower" gives: · 拼接 "sun" 和 "flower" 得:
Concatenation joins them in order into one string. · 拼接把它们按顺序连成一个字符串。
What is the length of the string "cat"? · 字符串 "cat" 的长度是多少?
It has 3 characters: c, a, t. · 它有 3 个字符:c、a、t。
String operations
- Concatenation 拼接 joins strings into one: "sun" + "flower" gives "sunflower".
- Length 长度 is how many characters a string holds — the length of "cat" is 3.
- A substring 子串 is a part of a string: from "computer" you can take "put".
- You can also read one character by its position.
字符串操作
- 拼接(concatenation)把字符串连成一个:"sun" + "flower" 得 "sunflower"。
- 长度(length)是字符串含多少字符——"cat" 的长度是 3。
- 一个子串(substring)是字符串的一部分:从 "computer" 你能取 "put"。
- 你也能按位置读一个字符。
Index and slice a string · 索引和切片一个字符串
Every character has a position (index from 0). A slice takes characters from the start index up to — but not including — the end index. · 每个字符有一个位置(从 0 索引)。一个切片取从起始索引到——但不包括——结束索引的字符。
If counting starts at 0, the character at position 0 of "cat" is: · 如果从 0 开始计数,"cat" 位置 0 的字符是:
Position 0 is the first character, "c". · 位置 0 是第一个字符,"c"。
A part of a string, such as "put" from "computer", is called a . · 字符串的一部分,如从 "computer" 取的 "put",叫一个。
A substring is a slice of the original string. · 子串是原字符串的一个切片。
Positions and indexing
- Positions are counted from a starting number.
- Many languages start at $0$, so in "cat" position $0$ is "c" and position $2$ is "t".
- A substring takes characters from a start index up to — but not including — an end index.
- Always check where a language starts counting.
位置与索引
- 位置从一个起始数字开始计数。
- 许多语言从 $0$ 开始,所以在 "cat" 中位置 $0$ 是 "c",位置 $2$ 是 "t"。
- 一个子串取从起始索引到——但不包括——结束索引的字符。
- 总要检查一门语言从哪里开始计数。
Concatenating "Hi, " + "Ana" + "!" produces: · 拼接 "Hi, " + "Ana" + "!" 产生:
The three strings join in order to greet the user. · 三个字符串按顺序连接来问候用户。
Processing user input
- These operations process user input 用户输入 — text a user types, like a name.
- A program might read a name, find its length, and greet the user by joining strings.
Greeting. Read the name "Ana" into name; its length is 3. Build the greeting by concatenation: "Hi, " + name + "!" gives "Hi, Ana!" — the output shown to the user.
处理用户输入
- 这些操作处理用户输入(user input)——用户键入的文本,像一个名字。
- 一个程序可能读一个名字、求它的长度,并通过连接字符串来问候用户。
问候。 把名字 "Ana" 读进 name;它的长度是 3。通过拼接构建问候语:"Hi, " + name + "!" 得 "Hi, Ana!"——显示给用户的输出。
A string is an ordered sequence of characters, so order matters. Key operations are concatenation (join), length (count), and substring (a part). Positions are usually indexed from 0, and a slice runs up to but not including the end index. Strings let a program process user input.
一个字符串是一个有序序列的字符,所以顺序重要。关键操作是拼接(连接)、长度(计数)和子串(一部分)。位置通常从 0 索引,一个切片取到但不包括结束索引。字符串让程序处理用户输入。