iOS 字符串筛选特殊字符
时间: 2023-05-20 13:05:43 浏览: 115
你可以使用 NSCharacterSet 类来筛选特殊字符,具体实现可以参考以下代码:
NSString *originalString = @"Hello, world! 🌍";
NSCharacterSet *specialCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"🌍"];
NSString *filteredString = [[originalString componentsSeparatedByCharactersInSet:specialCharacterSet] componentsJoinedByString:@""];
NSLog(@"%@", filteredString);
这段代码可以将字符串中的特殊字符 "🌍" 进行筛选,输出结果为 "Hello, world! "。
相关问题
iOS 字符串替换Html中某个div
可以使用正则表达式来替换Html中某个div的内容。
示例代码:
```
NSString *htmlString = @"<html><body><div id=\"myDiv\">Hello world!</div></body></html>";
NSString *newContent = @"New content";
NSString *divId = @"myDiv";
// 使用正则表达式替换div内容
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"<div\\s+id\\s*=\\s*\"%@\"[^>]*>.*?</div>", divId] options:NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators error:&error];
if (!error) {
NSString *modifiedString = [regex stringByReplacingMatchesInString:htmlString options:0 range:NSMakeRange(0, [htmlString length]) withTemplate:[NSString stringWithFormat:@"<div id=\"%@\">%@</div>", divId, newContent]];
NSLog(@"%@", modifiedString);
}
```
解释:
1. 定义Html字符串、新内容和div的id。
2. 创建一个正则表达式,用于匹配 div 标签中的内容。
3. 使用正则表达式替换 div 中的内容。
4. 输出替换后的Html字符串。
ios 公钥字符串 rsa
iOS 中使用 RSA 加密和解密需要使用到公钥和私钥,其中公钥可以转换为字符串形式。具体步骤如下:
1. 导入 Security.framework 框架
2. 创建 SecKeyRef 对象
```objc
- (SecKeyRef)getPublicKey {
NSString *publicKeyString = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcNwJztMj+UyIeHJv6X9F6n/2/2jD6UxvTmJwY8C3q1zDfjTJbX9eW1aLlZa+RfCv5GJjW8VgJdMfR7Vv0X2O4tj3qJ4E4tVvzj+Kg8VQaQ2bPL7Jv5X5b5g5Z5hGp5x5YgldfM6Jy1e+HJGcE0WgDvJIDBj6UO2L0ZwY8D2Q0z0TJr+Iqz1W8lGcC9EwwIDAQAB";
NSData *data = [[NSData alloc] initWithBase64EncodedString:publicKeyString options:NSDataBase64DecodingIgnoreUnknownCharacters];
if (!data) {
return nil;
}
NSString *tag = @"com.example.publickey";
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
(__bridge id)kSecClassKey, (__bridge id)kSecClass,
(__bridge id)kSecAttrKeyTypeRSA, (__bridge id)kSecAttrKeyType,
tag, (__bridge id)kSecAttrApplicationTag,
(__bridge id)kSecAttrKeyClassPublic, (__bridge id)kSecAttrKeyClass, nil];
SecKeyRef keyRef = SecKeyCreateWithData((__bridge CFDataRef)data, (__bridge CFDictionaryRef)attributes, nil);
return keyRef;
}
```
3. 将 SecKeyRef 对象转换为公钥字符串
```objc
- (NSString *)getPublicKeyString {
SecKeyRef keyRef = [self getPublicKey];
if (!keyRef) {
return nil;
}
NSData *data = [self dataFromKey:keyRef];
NSString *base64String = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
return base64String;
}
- (NSData *)dataFromKey:(SecKeyRef)key {
CFDataRef dataRef = SecKeyCopyExternalRepresentation(key, nil);
if (!dataRef) {
return nil;
}
NSData *data = (__bridge NSData *)dataRef;
CFRelease(dataRef);
return data;
}
```
以上代码将公钥字符串转换为 SecKeyRef 对象,然后再将 SecKeyRef 对象转换为 NSData 对象,最终使用 base64 编码将 NSData 对象转换为字符串形式。