Computer Programming 简明教程
Computer Programming - Strings
在我们关于 characters 的讨论中,我们了解到字符数据类型处理单个字符,您可以将键盘上的任何字符分配给字符类型变量。
During our discussion about characters, we learnt that character data type deals with a single character and you can assign any character from your keyboard to a character type variable.
现在,让我们稍微超前一点,并考虑一种情况,我们需要将多个字符存储在一个变量中。我们已经看到,C 编程不允许在一个字符类型变量中存储多个字符。因此,以下语句在 C 编程中无效并产生语法错误 −
Now, let’s move a little bit ahead and consider a situation where we need to store more than one character in a variable. We have seen that C programming does not allow to store more than one character in a character type variable. So the following statements are invalid in C programming and produce syntax errors −
char ch1 = 'ab';
char ch2 = '10';
我们还已经看到如何使用 arrays 的概念在变量中存储多个类似数据类型的值。以下是存储和打印数组中五个 int 类型数字的语法 −
We have also seen how to use the concept of arrays to store more than one value of similar data type in a variable. Here is the syntax to store and print five numbers in an array of int type −
#include <stdio.h>
main() {
int number[5] = {10, 20, 30, 40, 50};
int i = 0;
while( i < 5 ) {
printf("number[%d] = %d\n", i, number[i] );
i = i + 1;
}
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
number[0] = 10
number[1] = 20
number[2] = 30
number[3] = 40
number[4] = 50
现在,让我们像数字一样定义一个包含五个字符的数组,并尝试打印它们 −
Now, let’s define an array of five characters in the same way as we did for numbers and try to print them −
#include <stdio.h>
main() {
char ch[5] = {'H', 'e', 'l', 'l', 'o'};
int i = 0;
while( i < 5 ) {
printf("ch[%d] = %c\n", i, ch[i] );
i = i + 1;
}
}
此处,我们使用 %c 来打印字符值。当上述代码被编译和执行后,将产生以下结果 −
Here, we used %c to print character value. When the above code is compiled and executed, it produces the following result −
ch[0] = H
ch[1] = e
ch[2] = l
ch[3] = l
ch[4] = o
如果你已经完成了上述示例,我想你已经理解了字符串在 C 编程中的工作原理,因为 strings in C are represented as arrays of characters 。C 编程简化了字符串的分配和打印。让我们用简化的语法再检查同样的示例一次 −
If you are done with the above example, then I think you understood how strings work in C programming, because strings in C are represented as arrays of characters. C programming simplified the assignment and printing of strings. Let’s check the same example once again with a simplified syntax −
#include <stdio.h>
main() {
char ch[5] = "Hello";
int i = 0;
/* Print as a complete string */
printf("String = %s\n", ch);
/* Print character by character */
while( i < 5 ) {
printf("ch[%d] = %c\n", i, ch[i] );
i = i + 1;
}
}
此处,我们使用 %s 使用数组名称 ch 打印完整的字符串值,实际上,它是保存 ch 变量的内存地址的开头,如下所示 −
Here, we used %s to print the full string value using array name ch, which is actually the beginning of the memory address holding ch variable as shown below −
虽然从上述示例中看不到,但在 C 程序中将 '\0' 空字符内部分配为每个字符串的最后一个字符。它指示字符串的末尾,意味着如果你想在数组中存储一个 5 个字符的字符串,那么你必须定义一个 6 的数组大小作为良好实践,尽管 C 不会对此提出抱怨。
Although it’s not visible from the above examples, a C program internally assigns null character '\0' as the last character of every string. It indicates the end of the string and it means if you want to store a 5 character string in an array, then you must define an array size of 6 as a good practice, though C does not complain about it.
如果上述代码被编译和执行,将产生以下结果 −
If the above code is compiled and executed, it produces the following result −
String = Hello
ch[0] = H
ch[1] = e
ch[2] = l
ch[3] = l
ch[4] = o
Basic String Concepts
基于上述讨论,我们可以总结 C 编程语言中有关字符串的以下重要要点 −
Based on the above discussion, we can conclude the following important points about strings in C programming language −
-
Strings in C are represented as arrays of characters.
-
We can constitute a string in C programming by assigning character by character into an array of characters.
-
We can constitute a string in C programming by assigning a complete string enclosed in double quote.
-
We can print a string character by character using an array subscript or a complete string by using an array name without subscript.
-
The last character of every string is a null character, i.e., ‘\0’.
-
Most of the programming languages provide built-in functions to manipulate strings, i.e., you can concatenate strings, you can search from a string, you can extract sub-strings from a string, etc. For more, you can check our detailed tutorial on C programming or any other programming language.
Strings in Java
尽管你可以使用字符数组来存储字符串,但 Java 是一个高级编程语言,其设计人员尝试提供附加功能。Java 提供字符串作为内置数据类型,如同任何其他类型一样。这意味着你可以直接定义字符串,而不是将它们定义为字符数组。
Though you can use character arrays to store strings, but Java is an advanced programming language and its designers tried to provide additional functionality. Java provides strings as a built-in data type like any other data type. It means you can define strings directly instead of defining them as array of characters.
以下是用 Java 编写的等价程序。Java 使用 new 运算符来创建字符串变量,如下面的程序所示。
Following is the equivalent program written in Java. Java makes use of the new operator to create string variables as shown in the following program.
您可以尝试执行以下程序以查看输出:
You can try to execute the following program to see the output −
public class DemoJava {
public static void main(String []args) {
String str = new String("Hello");
System.out.println( "String = " + str );
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
String = Hello
Strings in Python
在 Python 中创建字符串就像使用单引号或双引号将字符串分配给 Python 变量一样简单。
Creating strings in Python is as simple as assigning a string into a Python variable using single or double quotes.
下面是一个简单的程序,用于创建两个字符串并使用 print() 函数打印它们 −
Given below is a simple program that creates two strings and prints them using print() function −
var1 = 'Hello World!'
var2 = "Python Programming"
print "var1 = ", var1
print "var2 = ", var2
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
var1 = Hello World!
var2 = Python Programming
Python 不支持字符类型;这些被当作长度为一的字符串处理,故而也被视作子字符串。
Python does not support character type; these are treated as strings of length one, thus also considered a substring.
要访问子字符串,请使用方括号进行切片,同时使用索引获得你的子字符串。看一看以下代码片段 −
To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. Take a look at the following code segment −
var1 = 'Hello World!'
var2 = "Python Programming"
print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]
执行上述代码后,将生成以下结果 −
When the above code is executed, it produces the following result −
var1[0]: H
var2[1:5]: ytho