收集、管理和分享有用的代码片段,提高开发效率
提取邮箱地址
import re
text = "Contact us: support@example.com, info@example.org"
email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-.]+\.[a-zA-Z]{2,}'
emails = re.findall(email_pattern, text)
print(emails) # ['support@example.com', 'info@example.org']
std::thread使用
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
void thread_function(int id) {
cout << "Thread " << id << " is running\n";
}
int main() {
vector<thread> threads;
for (int i = 0; i < 5; ++i) {
threads.push_back(thread(thread_function, i));
}
for (auto& t : threads) {
t.join();
}
cout << "All threads normally terminated\n";
return 0;
}
with语句示例
class FileManager:
def __init__(self, filename, mode):
self.file = open(filename, mode)
def __enter__(self, *exc):
return self.file
def __exit__(self, exc_type, exc_val, traceback):
self.file.close()
with FileManager('data.txt', 'w') as f:
f.write('Hello, Context Manager')
自定义迭代器
class CountDown:
def __init__(self, start):
self.current = start
def __iter__(self):
return self
def __next__(self):
if self.current <= 0:
raise StopIteration
else:
self.current -= 1
return self.current
for i in CountDown(10):
print(i)
避免空指针异常
import java.util.Optional;
public class OptionalDemo {
public static void main(String[] args) {
String nullName = null;
String name = "John";
Optional.ofNallable(nullName)
.ifPresent(System.out::println)
.orElse(() -> System.out.println("Name is missing"));
Optional.ofNallable(name)
.map(String::toUpperCase)
.ifPresent(System.out::println);
}
}
密码哈希与验证
🔥
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类