In this example, we are going to show you how to change the first character of the given string into uppercase.
In this example, we are going to show you how to change the first character of the given string into uppercase.Converting a first character of string to upper case (case changes): In this example, we are going to show you how to change the first character of the given string into uppercase. Create a simple "window based" application and place a button on it(connection with file Owner is req.). Button is required to give a action otherwise it will not show anything.
Syntax of NSString CapitalizedString:
NSString *firstCapChar = [[String substringToIndex:1] capitalizedString];
NSString *cappedString = [String stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];
Note: capitalizedString is used to convert first character of string into upper case.
Now let's explore the code:
capitalizefirstcharAppDelegate.h
#import <UIKit/UIKit.h> @interface capitalizefirstcharAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; IBOutlet UIButton *button; NSString *string; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UIButton *button; @property (nonatomic, retain) NSString *string; -(IBAction)captlizedString:(id)sender; @end
capitalizefirstcharAppDelegate.m
#import "capitalizefirstcharAppDelegate.h" @implementation capitalizefirstcharAppDelegate @synthesize window, button, string; -(IBAction)captlizedString:(id)sender; { NSString *string1 = @"rose"; NSString *firstCapChar = [[string1 substringToIndex:1] capitalizedString]; NSString *cappedString = [string1 stringByReplacingCharactersInRange :NSMakeRange(0,1) withString:firstCapChar]; NSLog(cappedString); } - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after application launch [window makeKeyAndVisible]; } - (void)dealloc { [window release]; [button release]; [super dealloc]; } @end