解析字符串

Objective-C

Posted by YiMiTuMi on August 15, 2023

解析字符串

这里解析以 “;” 或 “ ”号分割的字符串:
- (NSString*) FormatNsstring:(NSString*)strInfo
{
   
    NSMutableString* strRet = [[NSMutableString alloc] init];
    NSMutableString* strRealmNameAndIp = [[NSMutableString alloc] init];
    
    for (int i = 0; i < strInfo.length; i++)
    {
        unichar ch = [strInfo characterAtIndex: i];
        
        if (ch == ';' || ch == '|')
        {
            NSLog(@" strRealmNameAndIp: %@", strRealmNameAndIp);
        
            //拼接上分割符
            [strRet appendFormat : @"%C", ch];
            
            //清空
            [strRealmNameAndIp deleteCharactersInRange:NSMakeRange(0, strRealmNameAndIp.length)];
        }
        else
        {
            [strRealmNameAndIp appendFormat : @"%C", ch];
        }
    }
    
    //最后一个
    [strRet appendString : strTempInfo];
    
    return [[NSString alloc] initWithString:strRet];
}

注意:NSMutableString 使用前一定要初始化,不然所有有关NSMutableString的操作都会报错。