NSString

作者: 林希品 | 来源:发表于2021-11-10 09:04 被阅读0次

    字符串反转

    第一种:

    - (NSString *)reverseWordsInString:(NSString *)str

    {    

        NSMutableString *newString = [[NSMutableString alloc] initWithCapacity:str.length];

        for (NSInteger i = str.length - 1; i >= 0 ; i --)

        {

            unichar ch = [str characterAtIndex:i];       

            [newString appendFormat:@"%c", ch];    

        }    

         return newString;

    }

    //第二种:

    - (NSString*)reverseWordsInString:(NSString*)str

    {    

         NSMutableString *reverString = [NSMutableString stringWithCapacity:str.length];    

         [str enumerateSubstringsInRange:NSMakeRange(0, str.length) options:NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences  usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

              [reverString appendString:substring];                         

          }];    

         return reverString;

    }

    iOS 获取汉字的拼音

    + (NSString *)transform:(NSString *)chinese

    {    

        //将NSString装换成NSMutableString

        NSMutableString *pinyin = [chinese mutableCopy];    

        //将汉字转换为拼音(带音标)    

        CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);    

        NSLog(@"%@", pinyin);    

        //去掉拼音的音标    

        CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);    

        NSLog(@"%@", pinyin);    

        //返回最近结果    

        return pinyin;

    }

    G: 公元时代,例如AD公元

    yy: 年的后2位

    yyyy: 完整年

    MM: 月,显示为1-12

    MMM: 月,显示为英文月份简写,如 Jan

    MMMM: 月,显示为英文月份全称,如 Janualy

    dd: 日,2位数表示,如02

    d: 日,1-2位显示,如 2

    EEE: 简写星期几,如Sun

    EEEE: 全写星期几,如Sunday

    aa: 上下午,AM/PM

    H: 时,24小时制,0-23

    K:时,12小时制,0-11

    m: 分,1-2位

    mm: 分,2位

    s: 秒,1-2位

    ss: 秒,2位

    S: 毫秒

    阿拉伯数字转中文格式

    +(NSString *)translation:(NSString *)arebic

    {  

        NSString *str = arebic;

        NSArray *arabic_numerals = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];

        NSArray *chinese_numerals = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"零"];

        NSArray *digits = @[@"个",@"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"];

        NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chinese_numerals forKeys:arabic_numerals];

        NSMutableArray *sums = [NSMutableArray array];

        for (int i = 0; i < str.length; i ++) {

            NSString *substr = [str substringWithRange:NSMakeRange(i, 1)];

            NSString *a = [dictionary objectForKey:substr];

            NSString *b = digits[str.length -i-1];

            NSString *sum = [a stringByAppendingString:b];

            if ([a isEqualToString:chinese_numerals[9]])

            {

                if([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]])

                {

                    sum = b;

                    if ([[sums lastObject] isEqualToString:chinese_numerals[9]])

                    {

                        [sums removeLastObject];

                    }

                }else

                {

                    sum = chinese_numerals[9];

                }

                if ([[sums lastObject] isEqualToString:sum])

                {

                    continue;

                }

            }

            [sums addObject:sum];

        }

        NSString *sumStr = [sums componentsJoinedByString:@""];

        NSString *chinese = [sumStr substringToIndex:sumStr.length-1];

        NSLog(@"%@",str);

        NSLog(@"%@",chinese);

        return chinese;

    }

    获取手机安装的应用

    Class c =NSClassFromString(@"LSApplicationWorkspace");

    id s = [(id)c performSelector:NSSelectorFromString(@"defaultWorkspace")];

    NSArray *array = [s performSelector:NSSelectorFromString(@"allInstalledApplications")];

    for (id item in array)

    {

        NSLog(@"%@",[item performSelector:NSSelectorFromString(@"applicationIdentifier")]);

        //NSLog(@"%@",[item performSelector:NSSelectorFromString(@"bundleIdentifier")]);

        NSLog(@"%@",[item performSelector:NSSelectorFromString(@"bundleVersion")]);

        NSLog(@"%@",[item performSelector:NSSelectorFromString(@"shortVersionString")]);

    }

    iOS-身份证中间数字设置为星号

    - (NSString*)replaceStringWithAsterisk:(NSInteger)startLocation length:(NSInteger)length {

       NSString*replaceStr =self;

       for(NSIntegeri =0; i < length; i++) {

           NSRangerange =NSMakeRange(startLocation,1);

            replaceStr = [replaceStr stringByReplacingCharactersInRange:range withString:@"*"];

            startLocation ++;

        }

       returnreplaceStr;

    }

    NSString转char类型数组

    unsignedcharcss[aaa.length];

        memcpy(css, [aaa cStringUsingEncoding:NSUTF8StringEncoding], aaa.length);

       for(inti =0; i <sizeof(css); i++) {

           NSLog(@"%d======%c",i,css[i]);

        }

    char数组转NSString,这里就直接遍历拼接

    NSMutableString*mustr = [NSMutableStringstring];

       for(inti =0; i <sizeof(css); i++) {

            [mustr appendFormat:@"%c",css[i] ];

        }

       NSLog(@"%@",mustr);

    0.按范围截取字符串

    NSString *string1 = @"This is a string";  

    NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];  //从0位开始截取4个的长度

    1.截取字符串

      NSString*string =@"sdfsfsfsAdfsdf";

     string = [string substringToIndex:7];//截取前七位字符 (第七位不算在内)

     [string substringFromIndex:2];//字符串从第2位开始截取 (包含第二位)

    2.替换字符串

     NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@"我要被替换了" withString:@"我来替换你"];

    3.中文字符串转UTF8

     NSString *strB = [@"中国"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    4.将NSData和字符串

     //将NSData转化为NSString

      NSString* str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

     //将NSString 转化为NSData (NSString.h)

     NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

    5.从文件读取字符串

     NSString *path = @"astring.text";  

     NSString *astring = [[NSString alloc] initWithContentsOfFile:path]; 

    6.写字符串到文件

        NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];  

        NSString *path = @"astring.text";      

       [astring writeToFile: path atomically: YES];  

    7.字符串中搜索字符串

             if ([strArangeOfString:area].location !=NSNotFound) {

                //如果strA的内容中包含area那么进入判断

             }

    8.在需要的位置插入字符串-恩插入

     NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];  

    [String1 insertString:@"Hi! " atIndex:0];  

    9.切割字符串

     NSArray *array = [nstring componentsSeparatedByString:@","];

    10.转换 NSURLRequest 或 URL 为字符串

     NSURL *url = [requestURL];

     NSString *str=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //你也可以用点属性

     NSString *urlStr = request.URL.absoluteString;

    11.对比字符串的头

     NSString *str1 = @"aaaa://";

    NSString *str2 = @"aaaa://aaaa.vvvv.com";

     BOOL _is = [str2 hasPrefix:str1];

    12数字每隔3位用逗号分隔

    /**

    价格转换为每隔3位用逗号分割

    show 是否显示小数点后面

    */

    + (NSString *)changePriceWithNumber:(float)value showPoint:(BOOL)show {

        NSString *valueStr = @"";

        NSString *format = @"";

        if (show) {

            valueStr = [NSString stringWithFormat:@"%.2f", value];

            format = @",###.##";

        } else {

            valueStr = [NSString stringWithFormat:@"%.f", value];

            format = @",###";

        }

        NSDecimalNumber *decNumber = [NSDecimalNumber decimalNumberWithString:valueStr];

        NSNumberFormatter *numberFormatter =   [[NSNumberFormatter alloc] init];

        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

        [numberFormatter setPositiveFormat:format];

        return [numberFormatter stringFromNumber:decNumber];

    }

    相关文章

      网友评论

          本文标题:NSString

          本文链接:https://www.haomeiwen.com/subject/yyalzltx.html