Some methods of IOS about NSString

Source: Internet
Author: User

Some of the string application methods that are organized in the project can be encapsulated in a single class for invocation and are constantly updated to add:

1. The numbers are converted into corresponding Chinese numerals (the chapter number of the course grading catalogue in the project is used)

Excerpt from: http://blog.csdn.net/it_ds/article/details/47128563

+ (NSString *) Translation: (NSString *) arebic{nsstring *str = arebic;    Nsarray *arabic_numerals = @[@ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "0"];    Nsarray *chinese_numerals = @[@ "One", @ "two", @ "three", @ "four", @ "five", @ "six", @ "seven", @ "eight", @ "nine" @ "0"];    Nsarray *digits = @[@ "A", @ "ten", @ "hundred", @ "thousand", @ "Million", @ "ten", @ "hundred", @ "thousand", @ "Million", @ "ten", @ "hundred", @ "thousand", @ "Mega"];    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;}

2. Decide whether to include Chinese (video URL to determine if transcoding is required)

+ (BOOL) Ischinese: (NSString *) str{for    (int i=0; i<str.length; ++i)    {        Nsrange range = Nsmakerange (i, 1); c4/>nsstring *substring = [str substringwithrange:range];        const char *cstring = [subString utf8string];        if (strlen (cString) = = 3)        {            return YES;        }    }    return NO;}

3. Remove special characters (used when "/" in time stamp is removed)

+ (NSString *) stringdeletestring: (NSString *) str{    nsmutablestring *str1 = [nsmutablestring stringwithstring:str]; for    (int i = 0; i < str1.length; i++) {        Unichar c = [str1 characteratindex:i];        Nsrange range = Nsmakerange (i, 1);        if (c = = '/' | | c = = '-' | | c = = '. ' | | c = = ', ' | | c = = ' (' | | c = = ') ') {//* This can be any character            [str1 Deletecharactersinrange : Range];            -----;}    }    NSString *newstr = [NSString stringwithstring:str1];    return NEWSTR;}

4. Special character escaping (TextView to interface with "&" cannot be successful because the interface parameter begins with "&" The solution is that the mobile side first escapes the input and then turns the background one more time)

+ (NSString *) replacemystring: (NSString *) textstr{//NSString *inputstring = [[NSString alloc]init];    NSString *inputstring = textstr;    InputString = [InputString stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];    InputString = [inputstring stringbyreplacingoccurrencesofstring:@ "&" withstring:@ "&"];    InputString = [InputString stringbyreplacingoccurrencesofstring:@ "<" withstring:@ "<"];    InputString = [inputstring stringbyreplacingoccurrencesofstring:@ ">" withstring:@ ">"];    InputString = [inputstring stringbyreplacingoccurrencesofstring:@ "" withstring:@ " "];    InputString = [inputstring stringbyreplacingoccurrencesofstring:@ "\ '" withstring:@ "'"];    InputString = [inputstring stringbyreplacingoccurrencesofstring:@ "\" "withstring:@" ""];    InputString = [InputString stringbyreplacingoccurrencesofstring:@ "\ r" withstring:@ ""];  InputString = [inputstring stringbyreplacingoccurrencesofstring:@ "\ n" withstring:@ "<br/>"];  return inputstring;} 

 5. Converts the number of seconds to hours, minutes (the length of time that the interface returns is a unit of seconds, such as 3600 when you need to turn into hour and minute styles)

method to modify Time division + (NSString *) Timeformatfromseconds: (int) seconds{if (seconds >) {nsstring *str_hour = [NSString        stringwithformat:@ "%d", seconds/3600];        NSString *str_minute = [NSString stringwithformat:@ "%d", (seconds%3600)/60];            if ([Str_hour isequaltostring:@ "0"]) {int minutint = (seconds%3600)%60;                if (Minutint! = 0) {nsstring *format_time = [NSString stringwithformat:@ "%d", [Str_minute intvalue]+1];            return format_time;                }else{nsstring *format_time = [NSString stringwithformat:@ "%@", Str_minute];            return format_time;            }}else {int minutint = (seconds%3600)%60; if (Minutint! = 0) {nsstring *format_time = [NSString stringwithformat:@ "%@ when%d", Str_hour,[str_minute Intva                LUE]+1];            return format_time; }else{nsstring *format_time = [NSString stringwithformat:@]%@%@ ", Str_hour,str_minute];            return format_time;    }}}else if (seconds = = 0) {return @ "0";    } else{return @ "1"; }}

6. Get the size of the file (used when downloading the course video to get the size)

Ways to return file size
+ (NSString *) getfilesizestring: (cgfloat) size
{
if (size>1024*1024*1024) {
return [NSString stringwithformat:@ "%.1FG", size/1024/1024/1024];//greater than 1G, convert to G-Unit string
}
else if (size<1024*1024*1024&&size>=1024*1024)//greater than 1M, convert to M-unit string
{
return [NSString stringwithformat:@ "%.1FM", size/1024/1024];
}
else if (size>=1024&&size<1024*1024)//less than 1M, but more than 1KB, convert to KB units
{
return [NSString stringwithformat:@ "%.1FK", size/1024];
}
else//the remainder is less than 1K, it is converted into B units
{
return [NSString stringwithformat:@ "%.1FB", size];
}
}

7. Calculate the time difference (pre-save and the current time difference for the display of the course update times)

+ (NSString *) Comparecurrenttime: (nsdate*) comparedate{nstimeinterval timeinterval=[Comparedate Timeintervalsincenow]; TimeInterval= -timeinterval; Longtemp =0; NSString*result; NSLog (@"timeinterval:%f", timeinterval); if(TimeInterval < -) {result= [NSString stringWithFormat:@"just"]; }    Else if(temp = timeinterval/ -) < -) {result= [NSString stringWithFormat:@"%ld minutes ago", temp]; }    Else if(temp = temp/ -) < -) {result= [NSString stringWithFormat:@"%ld Small Front", temp]; }    Else if(temp = temp/ -) < -) {result= [NSString stringWithFormat:@"%ld days ago", temp]; }    Else if(temp = temp/ -) < A) {result= [NSString stringWithFormat:@"%ld months ago", temp]; }    Else{Temp= temp/ A; Result= [NSString stringWithFormat:@"%ld years ago", temp]; }    returnresult; }

8. String transcoding (useful for submitting data in the background)

String to 16 binary + (NSString *) Unicostr: (NSString *) text{    nsstring *hexstr = @ "";        for (int i=0;i< [text length];i++)    {        hexstr = [hexstr stringbyappendingformat:@ "%@", [NSString stringwithformat:@ "0x%1x", [text Characteratindex:i]];    }    return hexstr;} String to 8 binary + (NSString *) Eightunicostr: (NSString *) text{    nsstring *hexstr = @ "";//    int slen = strlen ([text Utf8string]);    for (int i = 0; i < text.length; i++)    {        //fffffff0 remove front six F & 0xFF        hexstr = [Hexstr stringbyappending format:@ "%@", [NSString stringwithformat:@ "0x%X", [Text Utf8string][i] & 0xFF];    }    return hexstr;}

Some methods of IOS about NSString

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.