字符串处理
处理 | Bash | C | C++ | Python | QT |
---|---|---|---|---|---|
字符串长度 | ${#str} | strlen() | size() | len() | — |
访问字符 | ${string:start:1} | a[12] | a[12],at() | a[12] | — |
查找子串位置 | ${string/substring} | strstr() | find() | s.find() | — |
切片 | ${string:start:length} | — | substr() | — | — |
替换 | ${string/substring/replacement} | — | replace() | s.replace() | — |
删除 | ${string//substring/} | a[12]=‘’ | erase() | — | |
拼接 | ${string1}${string2} | strcat() | += | s.join() | — |
转成为大写 | ${string^^} | toupper() | toupper() | s.upper() | — |
转换为小写 | ${string,} | tolower() | tolower() | s.lower() | — |
转换为整数 | — | atoi() | stoi() | — | — |
转换为字符 | — | sprintf() | to_string() | str() | — |
判断为空 | if [ “$str” == “” ] | if(a==“”) | if(a==“”) | — | — |
相等性判断 | if [ “$str1” == “$str2” ] | strcmp() | if(a==b) | — | — |
包含子串判断 | if [[ $str == *hello* ]] | strstr() | find() | s.find(substring) | — |
分割字符串 | cut -F: {print “$2”} | strtok() | getline(ss,s,‘;’) | s.split() | — |
去除首位空白 | — | — | — | s.strip() | — |
二进制转十进制 | — | — | — | — | — |
十六进制转十进制 | — | — | — | — | — |
十进制转二进制 | — | — | — | — | — |
十进制转十六进制 | — | — | — | — | — |
变量、数据类型
Bash:
# 字符串
a="hello"
# 布尔
a=true
# 整形
C/C++:
变量类型:
# 读写
int a=3;
# 只读
const a=3;
# 静态
static a=3;
基本数据类型:
// 整形: int、short、long、unsigned int、unsigned short、unsigned long
int a=0;
int a=0x80;
int a=0b10010;
int a=5ull;
int a=5e3; //科学计数法,5乘以10的3次幂
// 字符型: char
char data='a';
// 浮点型: float、double
float a=1.0;
// 布尔类型: bool
bool a=true
// 空类型: void
// 指针类型:(int *)等
自定义数据类型:
// 结构体: struct, class
struct Name{
int a;
int b;
};
// 枚举: enum
enum{red,yellow,green};
// 共用体: union
union Name{
int a;
char b;
};
Python
基本数据类型:
# 整形
a=0
a=0x80
a=0b10010
a=2.5e6
a=1_000
// 字符型
data='a';
data="abc"
// 浮点型
a=1.0;
// 布尔类型: bool
bool a=True
容器
Bash:
# 字符数组
a="1 2 3"
C:
// 数组
TYPE a[num];
C++:
// 动态数组
vector<TYPE> a;
// 双向链表
list<TYPE> a;
// 散列表,二叉平衡树
map<TYPE1,TYPE2> a;
// 字典,二叉平衡树
set<TYPE> a;
// 哈希散列表
unordered_map<TYPE> a;
// 哈希字典
unordered_set<TYPE> a;
// 队列
queue<TYPE> a;
// 栈
stack<TYPE> a;
Python:
# 列表,list
data=[1,'a']
# 元祖
data=(1,2,3)
# 字典
data={'a':1,'b':2}
# 集合
a=set()
循环、分支
Bash:
# if分支
if <conf>
then
command1
else
command2
fi
# case分支
case <variable> in
pattern1)
command1
;;
pattern2)
command2
;;
*)
command3
;;
esac
# for循环
for variable in values
do
command1
command2
...
done
# while循环
while condition
do
command1
command2
...
done
C/C++/QT:
// if分支
if (condition)
{
command1;
command2;
...
}
else
{
command3;
command4;
...
}
// switch-case分支
switch (variable)
{
case value1:
command1;
break;
case value2:
command2;
break;
...
default:
command3;
break;
}
// for循环
for (initialization; condition; increment)
{
command1;
command2;
...
}
// while循环
while (condition)
{
command1;
command2;
...
}
python:
# if分支
if condition:
command1
command2
...
else:
command3
command4
...
# for循环
for variable in sequence:
command1
command2
...
# while循环
while condition:
command1
command2
...
函数
Bash:
# 函数定义
function_name() {
commands
return value
}
# 函数调用
function_name arg1 arg2 ...
C/C++/QT:
// 函数定义
int hello(int a,int b) {
/* 函数体 */
return value;
}
// 函数调用
int result = hello(1, 2);
Python:
# 函数定义
def add(a, b):
"""将两个数字相加并返回结果"""
result = a + b
return result
# 函数调用
add(a,b)
类
Bash:
无。
C++:
class Name{
private:
int a;
public:
Name();
int hello(int,int);
};
Python:
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
def get_descriptive_name(self):
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
IO
Bash:
# 输入
read <变量>
# 输出
echo <内容>
C:
// 输入
scanf()
fread()
// 输出
printf()
fwrite()
C++:
// 输入
cin>>
istream
// 输出
cout<<
ostream
Python:
# 输入
input()
read()
# 输出
print()
write()