字符串
简介
- 字符串实际上是一个 unicode 字符数组。你可以执行几乎所有我们在数组中使用的操作。
- 然而,二者之间还是存在一些区别。字符串的特性在不同的语言之间可能有很大不同。
C++
与数组相比,我们可以对字符串执行一些额外的操作。
#include <iostream>
int main() {
string s1 = "Hello World";
// 1. concatenate
s1 += "!";
cout << s1 << endl;
// 2. find
cout << "The position of first 'o' is: " << s1.find('o') << endl;
cout << "The position of last 'o' is: " << s1.rfind('o') << endl;
// 3. get substr
cout << s1.substr(6, 5) << endl;
}
比较函数
字符串有它自己的比较函数。是否可以用 “==” 来比较两个字符串取决于该语言是否支持运算符重载。
#include <iostream>
int main() {
string s1 = "Hello World";
cout << "s1 is \"Hello World\"" << endl;
string s2 = s1;
cout << "s2 is initialized by s1" << endl;
string s3(s1);
cout << "s3 is initialized by s1" << endl;
// compare by '=='
cout << "Compared by '==':" << endl;
cout << "s1 and \"Hello World\": " << (s1 == "Hello World") << endl;
cout << "s1 and s2: " << (s1 == s2) << endl;
cout << "s1 and s3: " << (s1 == s3) << endl;
// compare by 'compare'
cout << "Compared by 'compare':" << endl;
cout << "s1 and \"Hello World\": " << !s1.compare("Hello World") << endl;
cout << "s1 and s2: " << !s1.compare(s2) << endl;
cout << "s1 and s3: " << !s1.compare(s3) << endl;
}
是否可变
不可变意味着一旦字符串被初始化,你就无法改变它的内容。在某些语言(如 C ++)中,字符串是可变的。 也就是说,你可以像在数组中那样修改字符串。在其他一些语言(如 Java)中,字符串是不可变的。 这会带来一些问题:比如不能做修改操作。concat操作会非常慢(需要复制)。如果你确实希望你的字符串是可变的,则可以将其转换为字符数组。 如果你经常必须连接字符串,最好使用一些其他的数据结构,如 StringBuilder 。
Python3
- Python字符串是不可变的。
1.将值转换为字符串并设置其格式
(1)使用字符串格式设置运算符——百分号
- 这个运算符的行为类似于C语言中的经典函数printf:在%左边指定一个格式字符串,在右边指定值。指定值时,可使用单个值(如字符串或数字),可使用元组,还可使用字典,其中最常见的是元组。
%s
称为转换说明符,指出了要将值插入什么地方。s
意味着将值视为【字符串】进行格式设置。如果指定的值不是字符串,将使用str
将其转换为字符串。其他说明符将导致其他形式的转换。例如,%.3f
将值的格式设置为包含3位小数的浮点数。
format = "Hello, %s. %s enough for ya?" #格式字符串
values = ('world', 'Hot') #值,以元组形式表示
format % values
#输出如下:
'Hello, world. Hot enough for ya?'
(2)字符串方法format
- 它融合并强化了早期方法的优点。使用这种方法时,每个替换字段都用花括号括起,其中可能包含名称,还可能包含格式设置的信息。
- 在最简单的情况下,替换字段没有名称或使用索引。
#替换字段是空的
>>> "{}, {} and {}".format("first", "second", "third")
'first, second and third'
#替换字段使用索引
>>> "{0}, {1} and {2}".format("first", "second", "third")
#索引可以无序
>>> "{3} {0} {2} {1} {3} {0}".format("be", "not", "or", "to")
'to be or not to be'
#命名替换字段
>>> from math import pi
>>> "{name} is approximately {value:.2f}.".format(value=pi, name="π")
'π is approximately 3.14.'
使用格式说明符的方法
例如:使用格式说明符.2f
,并使用冒号将其与字段名隔开。它意味着要使用包含2位小数的浮点数格式。
2.字符串方法
字符串的方法比列表要多得多,因为其很多方法都是从模块string那里“继承”而来的。这里只介绍一些最有用的。
#【1】center:通过在两边添加填充字符(默认为空格)让字符串居中。
>>> "The Middle by Jimmy Eat World".center(39)
' The Middle by Jimmy Eat World '
>>> "The Middle by Jimmy Eat World".center(39, "*")
'*****The Middle by Jimmy Eat World*****'
#【2】find:在字符串中查找子串。如果找到,就返回子串的第一个字符的索引,否则返回-1。
>>> title = "Monty Python's Flying Circus"
>>> title.find('Monty')
0
#【3】join:用于合并序列的元素。所合并序列的元素必须都是字符串。
>>> seq = ['1', '2', '3', '4', '5']
>>> sep.join(seq) # 合并一个字符串列表
'1+2+3+4+5'
>>> dirs = '', 'usr', 'bin', 'env'
>>> '/'.join(dirs)
#【4】lower:返回字符串的小写版本。
>>> 'Trondheim Hammer Dance'.lower()
'trondheim hammer dance'
#【5】replace:指定子串都替换为另一个字符串,并返回替换后的结果。
>>> 'This is a test'.replace('is', 'eez')
'Theez eez a test'
#【6】split:将字符串拆分为序列。如果没有指定分隔符,将默认在单个或多个连续的空白字符(空格、制表符、换行符等)处进行拆分。
>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
#【7】strip:将字符串开头和末尾的空白(但不包括中间的空白)删除,并返回删除后的结果
>>>' internal whitespace is kept '.strip()
'internal whitespace is kept'