iOS开发-- 字符串分割(多个字符分割一个字符串)、拼接

NSString * ptr = @”I am a man”;

//将字符串整体作为分割条件 返回值为NSArray不可变数组
NSArray * array = [ptr componentsSeparatedByString:@” “];

//若修改,则将NSArray转化为NSMutableArray
NSMutableArray * array1 = [NSMutableArray arrayWithArray:array];

//以字符串中的字符作为分割条件进行分割
NSArray * array2 = [ptr componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@”, “]];

《字符集合》
NSCharacterSet 这是个字符集合类

//把字符串转化为字符集合
[NSCharacterSet characterSetWithCharactersInString:@”, “]

———————字符串拼接实例———————
NSString * str = [array componentsJoinedByString:@” “];
若分割条件出现在开头或者结尾,则会出现空串@”“,如果不需要,则需要转化成NSMutableString对空串进行处理
func1: [array1 removeObject:@”“];
//找到空串直接删除
func2: for(id obj in array1){
if([obj length] == 0)
//空串的长度为0
if([obj isEqualToString:@”“])
//与空串进行比较(字符串是不能进行==比较的,要使用函数)
}

在 OC 中,说是有下面3种方法,
NSString *str=[NSString initWithFormat:@”%@,%@” , a , b];
NSString *str=[a stringByAppendingString: b];
NSString *str=[string stringByAppendingFormat:@”%@,%@”, a , b];

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中使用公钥字符串进行RSA加密的步骤如下: 1. 将公钥字符串转换为NSData类型。 ``` NSString *publicKeyString = @"-----BEGIN PUBLIC KEY-----\n...公钥字符串...\n-----END PUBLIC KEY-----"; NSData *publicKeyData = [publicKeyString dataUsingEncoding:NSUTF8StringEncoding]; ``` 2. 创建SecKey对象。 ``` NSMutableDictionary *publicKeyAttributes = [[NSMutableDictionary alloc] init]; [publicKeyAttributes setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType]; [publicKeyAttributes setObject:@(2048) forKey:(__bridge id)kSecAttrKeySizeInBits]; [publicKeyAttributes setObject:publicKeyData forKey:(__bridge id)kSecValueData]; [publicKeyAttributes setObject:(__bridge id)kSecAttrKeyClassPublic forKey:(__bridge id)kSecAttrKeyClass]; SecKeyRef publicKey; OSStatus status = SecItemAdd((__bridge CFDictionaryRef)publicKeyAttributes, (CFTypeRef *)&publicKey); ``` 3. 使用SecKey对象进行加密。 ``` NSData *plainData = [@"要加密的数据" dataUsingEncoding:NSUTF8StringEncoding]; size_t cipherBufferSize = SecKeyGetBlockSize(publicKey); uint8_t *cipherBuffer = malloc(cipherBufferSize); memset(cipherBuffer, 0, cipherBufferSize); OSStatus status = SecKeyEncrypt(publicKey, kSecPaddingPKCS1, [plainData bytes], [plainData length], cipherBuffer, &cipherBufferSize); NSData *encryptedData = [NSData dataWithBytesNoCopy:cipherBuffer length:cipherBufferSize]; ``` 4. 释放SecKey对象。 ``` CFRelease(publicKey); ``` 注意:在iOS中使用公钥加密时,需要使用PKCS#1填充模式(kSecPaddingPKCS1)。另外,如果公钥字符串中的换行符不是`\n`,需要将其替换为`\n`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值