您的位置:首页 > 移动开发 > Objective-C

Objective-C 常用字符串操作

2015-06-07 00:00 417 查看
//
//  main.m
//  KenshinCui
//
//  Created by hxd_mac on 15-6-7.
//  Copyright (c) 2015年 hxd198. All rights reserved.
//
#import <Foundation/Foundation.h>
//字符串初始化
void Test1()
{

char *str1 = "C String";

NSString *str2 = @"OC string";

NSString *str3 = [[NSString alloc] init];

str3 =@"oc string";

NSString *str4 = [[NSString alloc] initWithString:@"Objectiv-C string" ];

NSString *str5 = [[NSString alloc] initWithFormat:@"age is %i, name is %@", 26, @"str5"];

NSString *str6 = [[NSString alloc] initWithUTF8String:str1];

NSString *str7 = [NSString stringWithFormat:@"objective-c string"];

NSLog(@"str1 = %s, str2 = %@, str3 = %@, str4 = %@, str5 = %@, str6 = %@, str7 = %@", str1, str2, str3, str4, str5, str6, str7);
}
//字符串大小写转换和比较
void Test2()
{
NSLog(@"\"hello world\" to upper is %@",[@"hello world" uppercaseString]);

NSLog(@"\"hello world\" to lower is %@", [@"hello world" lowercaseString]);

NSLog(@"\"hello world\" to capitalize is %@", [@"hello world" capitalizedString]);

BOOL result = [@"abc" isEqualToString:@"Abc"];

NSLog(@"%d", result);

NSComparisonResult result2 = [@"abc" compare:@"aBC"];

if (result2 == NSOrderedAscending) {
NSLog(@"left < right");
} else if (result2 == NSOrderedDescending) {
NSLog(@"left > right");
} else {
NSLog(@"the same");
}
}
//字符串查询
void Test3()
{
NSLog(@"has preifx %i", [@"sun of shine" hasPrefix:@"sun"]);

NSLog(@"has suffix %i", [@"abcdefg" hasSuffix:@"fg"]);

NSRange range = [@"abcedefgh" rangeOfString:@"ced"];

if (range.location ==NSNotFound) {
NSLog(@"not found");
} else {
NSLog(@"range is %@", NSStringFromRange(range));
}
}
//字符串分割合并
void Test4()
{
NSLog(@"%@", [@"abcdefgh" substringFromIndex:3]);

NSLog(@"%@", [@"abcdefgh" substringToIndex:3]);

NSLog(@"%@", [@"abcdefgh" substringWithRange:NSMakeRange(3, 2)]);

NSString *string = @"abc.123.mp3";

NSLog(@"%@", [string componentsSeparatedByString:@"."]);

NSLog(@"%@", [[string componentsSeparatedByString:@"."] componentsJoinedByString:@":"]);
}
//其它操作
void Test5()
{
NSLog(@"%i", [@"12" intValue]);

NSLog(@"%zi", [@"深圳你好!" length]);

NSLog(@"%c", [@"abc" characterAtIndex:0]);
//转换成c语言字符串
const char *string = [@"123" UTF8String];

NSLog(@"%s", string);
}
int main(int argc, const char * argv[])
{
@autoreleasepool {

Test1();
Test2();
Test3();
Test4();
Test5();

}
return 0;
}
输出结果:
2015-06-07 15:11:47.030 KenshinCui[1369:303] str1 = C String, str2 = OC string, str3 = oc string, str4 = Objectiv-C string, str5 = age is 26, name is str5, str6 = C String, str7 = objective-c string
2015-06-07 15:11:47.058 KenshinCui[1369:303] "hello world" to upper is HELLO WORLD
2015-06-07 15:11:47.060 KenshinCui[1369:303] "hello world" to lower is hello world
2015-06-07 15:11:47.062 KenshinCui[1369:303] "hello world" to capitalize is Hello World
2015-06-07 15:11:47.063 KenshinCui[1369:303] 0
2015-06-07 15:11:47.065 KenshinCui[1369:303] left > right
2015-06-07 15:11:47.066 KenshinCui[1369:303] has preifx 1
2015-06-07 15:11:47.067 KenshinCui[1369:303] has suffix 1
2015-06-07 15:11:47.069 KenshinCui[1369:303] range is {2, 3}
2015-06-07 15:11:47.070 KenshinCui[1369:303] defgh
2015-06-07 15:11:47.072 KenshinCui[1369:303] abc
2015-06-07 15:11:47.074 KenshinCui[1369:303] de
2015-06-07 15:11:47.075 KenshinCui[1369:303] (
abc,
123,
mp3
)
2015-06-07 15:11:47.076 KenshinCui[1369:303] abc:123:mp3
2015-06-07 15:11:47.077 KenshinCui[1369:303] 12
2015-06-07 15:11:47.152 KenshinCui[1369:303] 5
2015-06-07 15:11:47.154 KenshinCui[1369:303] a
2015-06-07 15:11:47.155 KenshinCui[1369:303] 123
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  常用字符串