2020年3月20日 星期五

Basic String Manipulation in Objective-C



String append :



1.
- (NSString*) concatenateString:(NSString*)stringA withString:(NSString*)stringB
{
    NSString *finalString = [NSString stringWithFormat:@"%@%@", stringA,
                                                       stringB];
    return finalString;
}

2.
- (NSString*) appendingString:(NSString*)stringA withString:(NSString*)stringB
{
    NSString *finalString = [stringA stringByAppendingFormat:@"%@", stringB];
    return finalString;
}

String Length:

- (int) stringLength:(NSString*)string
{
    return [string length];
}

Remove text from string:

- (NSString*)remove:(NSString*)textToRemove fromString:(NSString*)input
{
   return [input stringByReplacingOccurrencesOfString:textToRemove
                                           withString:@""];
}

Uppercase / Lowercase / Titlecase:

- (NSString*)uppercase:(NSString*)stringToUppercase
{
   return [stringToUppercase uppercaseString];
}

- (NSString*)lowercase:(NSString*)stringToLowercase
{
   return [stringToLowercase lowercaseString];
}

- (NSString*)titlecase:(NSString*)stringToTitleCase
{
   return [stringToTitleCase capitalizedString];
}

Find/Replace : 

- (NSString*)findInString:(NSString*)string
        replaceWithString:(NSString*)stringToReplaceWith
{
   return [input stringByReplacingOccurrencesOfString:string
                                           withString:stringToReplaceWith];
}

Insert String :

- (NSString*)insertString:(NSString*)originalStr
        insertString:(NSString*)insertString loc:(NSUInteger)loc
{
  NSString *str1 =[originalStr substringToIndex:loc];
  NSString *str2 =[originalStr substringFromIndex:loc];
  NSString *finalString = [NSString stringWithFormat:@"%@%@%@", str1,insertString,str2];

  return finalString;
}

沒有留言:

張貼留言