Sunday, 20 March 2016

String Methods in iOS

  • Appending a String
  • String Padding
  • Range of String 
  • Substring with Range
  • Components Separated by String
  • Components Separated by Character in set
  • Components joined by String
  • String separated by comma (,)  added in array
  • Substring from String
  • Substring to Index
  • String by replacing occurrences of String
  • Case sensitive Comparison

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //Appending a String
        NSString *errortag=@ "Error: ";
        NSString *errorstring=@ "premature end of file";
        NSString *errormessage=[errortag stringByAppendingString:errorstring];
        NSLog(@ "%@",errormessage);
        
        //String Padding
        NSString *temp=@ "Akshay";
        temp= [temp stringByPaddingToLength:8 withString:@ "." startingAtIndex:0];
        NSLog(@ "%@",temp);
        //Error on setting the index to 1 when there is no space in the withString.
        
        temp=@ "Akshay";
        temp=[temp stringByPaddingToLength:10 withString:@ ". " startingAtIndex:1];
        NSLog(@ "%@",temp);
        
        //rangeOfString
        NSString *new=@ "Stefan";
        NSRange range=[new rangeOfString:@ "John"];
        NSLog(@ "Location : %lu Length: %lu",range.location,range.length);
        //Range starting from 0, doesn't make sense.
        
        //substringWithRange
        NSString *str=@ "DamonSalvetore";
        NSRange range1={0,6};
        NSString *str1=[str substringWithRange:range1];
        NSLog(@ "Substring with Range is %@",str1);
        NSString *str2=[str substringWithRange:NSMakeRange(5, 6)];
        NSLog(@ "Range is %@",str2.capitalizedString);
        
        //Components Seperate by String
        NSString *list=@ "Damon, Alina, Kathrine";
        NSArray *listitem= [list componentsSeparatedByString:@ ","];
        for (int i=0; i<listitem.count; i++) {
            NSLog(@ "%@",[listitem objectAtIndex:i]);
        }
        
        //Components seperated by character in set
        NSString *st=@ "A~B~C~D~E";
        NSArray *starr=[st componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@ "~"]];
        NSLog(@ "%@",starr);
        NSString *st1=@ "A~B^C~D^E";
        NSArray *starr1=[st1 componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@ "^~"]];
        NSLog(@ "%@",starr1);
        
        //Components Joined by String
        NSArray *pathArray=[NSArray arrayWithObjects:@ "here",@ "dragons", @ "are", @ "stupid", nil];
        NSLog(@ "%@",[pathArray componentsJoinedByString:@ " "]);
        
        //String seperated by comma(,) added in array, and showed as desired output.
        NSString *gon=@ "Dragons,are,smart";
        NSArray *path=[gon componentsSeparatedByString:@ ","];
        NSLog(@ "%@",[path componentsJoinedByString:@ " "]);
        
        //substring from Index
        NSString *sub=@ "Elina";
        NSLog(@ "%@",[sub substringFromIndex:3]);
        
        //substring to Index
        NSLog(@ "%@",[sub substringToIndex:3]);
        NSString *output=[sub substringToIndex:3];
        NSLog(@ "%@",[output stringByPaddingToLength:6 withString:@ "." startingAtIndex:0]);
        
        //string by replacing occurences of string
        NSLog(@ "%@",[sub stringByReplacingOccurrencesOfString:@ "hay" withString:@ "Emi"]);
        
        //case insensitive Comparision
        NSLog(@ "Case Insensitive : %lu",[sub caseInsensitiveCompare:@ "aaksha"]);
        NSLog(@ "Case Sensitive %lu",[sub compare:@ "Akshay"]);
        
        //is equal to String (Returns BOOL i.e. 1 or 0 | YES or NO)
        NSLog(@ "Comparision returns Bool value : %d",[sub isEqualToString:@ "Akshay"]);
        
        //BOOL
        NSLog(@ "Prefix : %d",[sub hasPrefix:@ "Caroline"]);
        NSLog(@ "Suffix : %d",[sub hasSuffix:@ "caroline"]);
    }
    return 0;

}

No comments:

Post a Comment

Read contacts in iOS using Swift with error handling

Just copy and paste the below code Pre-step before pasting the below code Add below line in AppDelegate.swift file below import statement ...