Foundation--NSString

雍马鲁
2023-12-01

创建字符串

NSString *aStirng = @"I'm a string!";

使用变量初始化

NSString *name = @”Chen”;

NSString *aString = [NSString stringWithFormat:@"My name is %@",name];

[aString release];

打开指定路径上的文本文件,读取文件内容,并使用内容初始化一个字符串

NSError *error = nil;
[[NSString alloc] initWithContentsOfFile:@"/tmp/words.txt" encoding:NSUTF8StringEncoding error:&error];
        
if (nil != error) {
    NSLog(@"Unable to read data from file, %@",[error localizedDescription]);
}
检查字符串是否以另一个字符串开头

- (BOOL)hasPrefix:(NSString *)aString;//是否以aString开头

NSString *String1 = @”NSStringInformation.txt”;

[String1 hasPrefix:@"NSString"] ? NSLog(@”YES”) : NSLog(@”NO”);

-   (BOOL)hasSuffix:(NSString *)aString;//是否以aStirng结尾

[String1 hasSuffix:@".txt"] ? NSLog(@”YES”) : NSLog(@”NO”);

检查是否包含其他字符

NSString *aString = [[NSStringalloc] initWithString:@”This is a String!”];

Boolean contains = [aString rangeOfString:@"This"].length>0;

从指定文件读取字符串

NSString *path = @”astring.txt”;

NSString *aString = [[NSStringalloc] initWithContentsOfFile:path];

[aString release];

写字符串到指定文件

NSString *aString = [[NSStringalloc] initWithString:@”This is a String!”];

NSString *path = @”astring.txt”;

[aString writeToFile: path atomically: YES];

[aString release];

比较两个字符串

/*比较两个字符串完全相等*/

- (BOOL)isEqualToString:(NSString )aString;

NSString *string1 = @”This is a string!”;

NSString *string2 = @”This is a string!”;

BOOL result = [string1 isEqualToString:string2];



/*三种返回值(NSOrderedAscending, NSOrderedSame, NSOrderedDescending) NSOrderedAscending表示左边小于右边,NSOrderedSame表示两边相等,NSOrderedDescending表示右边小于左边。26个字母越靠后越大*/

-   (NSComparisonResult)compare:(NSString *)string;//三种可能返回值

NSString *string1 = @”This is a string!”;

NSString *string2 = @”This is b string!”;

//result==YES

BOOL result = [string1 compare:string2] == NSOrderedAscending;

 

/*忽略大小写比较字符串,也返回三种返回值(NSOrderedAscending, NSOrderedSame, NSOrderedDescending),返回值意义参考compare方法*/

-    (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;

NSString *string1 = @”this is a string!”;

NSString *string2 = @”This is b String!”;

//result==YES

BOOL result = [string1 caseInsensitiveCompare:string2] == NSOrderedSame;

改变字符串大小写

NSString *string = @”STring”;

[string uppercaseString];//全部大写:STRING

[string lowercaseString];//全部小写:string

[string capitalizedString];//首字母大写:String

去掉字符串两端的空格

NSString *aString = @” string “;

NSLog(@”length:%d”,aString.length);//length:8

NSString *resultString = [aString stringByTrimmingCharactersInSet:

[NSCharacterSet whitespaceCharacterSet]];

NSLog(@”length:%d”,resultString.length);//length:6

去掉字符串两端的空格和回车

NSString *aString = @” string “;

NSString *resultString = [aString stringByTrimmingCharactersInSet:

[NSCharacterSet whitespaceAndNewlineCharacterSet]];

在字符串中搜索子串的长度和位置

NSString *string = @”This is a string!”;

NSString *subString = @”string”;

NSRange range = [string rangeOfString:subString];

intlocation = range.location;//10

int length = range.length;//6

if (range.length>0) {

//包含子串

}else{

//不包含子串

}

替换字符串

NSString *string = @”Hello,Objective-C!”;

NSString *new = [string stringByReplacingOccurrencesOfString:

@"Objective-C" withString:@"Swift"]; 

分割字符串成数组

NSString *string = @”a b c d e f”;

NSArray *resultArray = [string componentsSeparatedByString:@" "];

字符串数组拼接成字符串

NSArray *array = [NSArray arrayWithObjects:@"Hello",@"Swift",nil];

[pathArray componentsJoinedByString:@""];//HelloSwift 

抽取子串

/*从字符串开头一直截取到指定的位置,位置从0开始,但不包括该位置的字符*/

- (NSString *)substringToIndex:(NSUInteger)to;

NSString *string = @”This is a string”;

[string substringToIndex:4];//This

 

/*以指定位置开始(包括指定位置的字符),并包括之后的全部字符*/

-   (NSString *)substringFromIndex:(NSUInteger)from;

NSString *string = @”This is a string”;

[string substringFromIndex:4];//is a string

 

/*按照所给出的位置、长度,任意地从字符串中截取子串*/

-   (NSString *)substringWithRange:(NSRange)range;

NSString *string = @”This is a string”;

[string1 substringWithRange:NSMakeRange(0, 4)];//This
 类似资料: