Friday 22 August 2014

Category - Sample Code for iPhone

1. Create a File for Objective - C Category and Click the Next Button

2. Type a Name for Category and Select the Category on NSString and Click the Create Button.

3. The File is created as Category Type. The File Name like as NSString+Name

4. Write the Below Codes

NSString+Name.h File
#import <Foundation/Foundation.h>

@interface NSString (Validate)

-(NSString *)checkthestring:(NSString *)string_value;

-(NSString *)checkanotherstring:(NSString *)str_value;

-(NSString *)nullcheck:(NSString *)string_null_value;

-(int)getLength:(NSString*)string_length;

@end


NSString+Name.m File
#import "NSString+Validate.h"

@implementation NSString (Validate)

-(NSString *)checkthestring:(NSString *)string_value
{

// Special Characters
    string_value = [string_value stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n\r"];

    string_value = [string_value stringByReplacingOccurrencesOfString:@"\\r" withString:@"\n\r"];
    string_value = [string_value stringByReplacingOccurrencesOfString:@"&ldquo;" withString:@"'"];
    string_value = [string_value stringByReplacingOccurrencesOfString:@"&rdquo;" withString:@"'"];
    string_value = [string_value stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
    string_value = [string_value stringByReplacingOccurrencesOfString:@"&laquo;" withString:@"<<"];
    string_value = [string_value stringByReplacingOccurrencesOfString:@"&raquo;" withString:@">>"];
    string_value = [string_value stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    string_value = [string_value stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    string_value = [string_value stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    string_value = [string_value stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string_value = [string_value stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    
    // ISO 8859-1 Symbols
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&iexcl;" withString:@"¡"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&cent;" withString:@"¢"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&pound;" withString:@"£"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&curren;" withString:@"¤"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&yen;" withString:@"¥"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&brvbar;" withString:@"¦"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&sect;" withString:@"§"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&uml;" withString:@"¨"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Atilde;&copy;" withString:@"é"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ordf;" withString:@"ª"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&laquo;" withString:@"«"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&not;" withString:@"¬"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&shy;" withString:@"    "];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&reg;" withString:@"®"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&macr;" withString:@"¯"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&deg;" withString:@"°"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&plusmn;" withString:@"±"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&sup2;" withString:@"²"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&sup3;" withString:@"³"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&acute;" withString:@"´"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&micro;" withString:@"µ"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&para;" withString:@"¶"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&middot;" withString:@"·"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&cedil;" withString:@"¸"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&sup1;" withString:@"¹"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ordm;" withString:@"º"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&raquo;" withString:@"»"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&frac14;" withString:@"¼"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&frac12;" withString:@"½"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&frac34;" withString:@"¾"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&iquest;" withString:@"¿"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&times;" withString:@"×"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&divide;" withString:@"÷"];
    
    // ISO 8859-1 Characters
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Agrave;" withString:@"À"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Aacute;" withString:@"Á"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Acirc;" withString:@"Â"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Atilde;" withString:@"Ã"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Auml;" withString:@"Ä"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Aring;" withString:@"Å"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&AElig;" withString:@"Æ"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Ccedil;" withString:@"Ç"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Egrave;" withString:@"È"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Eacute;" withString:@"É"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Ecirc;" withString:@"Ê"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Euml;" withString:@"Ë"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Igrave;" withString:@"Ì"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Iacute;" withString:@"Í"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Icirc;" withString:@"Î"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Iuml;" withString:@"Ï"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ETH;" withString:@"Ð"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Ntilde;" withString:@"Ñ"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Ograve;" withString:@"Ò"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Oacute;" withString:@"Ó"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Ocirc;" withString:@"Ô"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Otilde;" withString:@"Õ"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Ouml;" withString:@"Ö"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Oslash;" withString:@"Ø"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Ugrave;" withString:@"Ù"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Uacute;" withString:@"Ú"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Ucirc;" withString:@"Û"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Uuml;" withString:@"Ü"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&Yacute;" withString:@"Ý"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&THORN;" withString:@"Þ"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&szlig;" withString:@"ß"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&agrave;" withString:@"à"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&aacute;" withString:@"á"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&acirc;" withString:@"â"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&atilde;" withString:@"ã"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&auml;" withString:@"ä"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&aring;" withString:@"å"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&aelig;" withString:@"æ"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ccedil;" withString:@"ç"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&egrave;" withString:@"è"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&eacute;" withString:@"é"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ecirc;" withString:@"ê"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&euml;" withString:@"ë"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&igrave;" withString:@"ì"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&iacute;" withString:@"í"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&icirc;" withString:@"î"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&iuml;" withString:@"ï"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&eth;" withString:@"ð"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ntilde;" withString:@"ñ"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ograve;" withString:@"ò"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&oacute;" withString:@"ó"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ocirc;" withString:@"ô"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&otilde;" withString:@"õ"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ouml;" withString:@"ö"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&oslash;" withString:@"ø"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ugrave;" withString:@"ù"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&uacute;" withString:@"ú"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&ucirc;" withString:@"û"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&uuml;" withString:@"ü"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&yacute;" withString:@"ý"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&thorn;" withString:@"þ"];
    string_value =  [string_value stringByReplacingOccurrencesOfString:@"&yuml;" withString:@"ÿ"];
    
    return string_value;
}

-(NSString *)checkanotherstring:(NSString *)str_value
{
    str_value = [str_value stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n\r"];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"\\r" withString:@"\n\r"];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"&ldquo;" withString:@"'"];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"&rdquo;" withString:@"'"];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"&laquo;" withString:@"<<"];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"&raquo;" withString:@">>"];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    str_value = [str_value stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    
    return str_value;
}

-(NSString *)nullcheck:(NSString *)string_null_value
{
    if ( [string_null_value isEqualToString:@""] || string_null_value.length == 0 || [string_null_value isEqualToString:@"<null>"] || string_null_value == NULL)
    {
        NSLog(@"Value is Empty");
        string_null_value = @"Value is Empty";
    }
    
    NSLog(@"%@", string_null_value);
    
    return string_null_value;
}

-(int)getLength:(NSString*)string_length
{
    int length = string_length.length;
    return length;
}
@end


//** The code has been placed some other .m file **//

SomeFile.m

-(void)replaceSpecialCharacters
{

int Country;

NSString  *Replace_String = @"";
sqlite3_stmt * Replace_String_pdt_statement;

// Country for code: India-8 Europe-4 (English Language)
// It’s No Need to Encode Strings.
if ( Country == 4 || Country == 8 )
            {
                // Without Encode the String - Used for Special Characters, ISO 8859-1 Symbols & ISO 8859-1 Characters
Replace_String = [NSString stringWithFormat:@"%s", sqlite3_column_text(Replace_String_pdt_statement, 1)];
                NSLog(@"%@", Replace_String);
                
                Replace_String = [Replace_String checkthestring:Replace_String];
            }

// Country for code: Other are Chinese, Korean & Japanese.
// It’s Supported only Encode Strings.
            else
            {
                // With Encode the Characters - Used for Only Special Characters
    Replace_String = [NSString stringWithCString:(char *)sqlite3_column_text(Replace_String_pdt_statement, 1) encoding:NSUTF8StringEncoding];
                NSLog(@"%@", Replace_String);
                
                Replace_String = [Replace_String checkanotherstring:Replace_String];
                
                int length = [Replace_String getLength:Replace_String];
                NSLog(@"%d", length);
            }

}

No comments: