- //
- // main.m
- // 字符串比较
- //
- // Created by Mac on 15/10/28.
- // Copyright (c) 2015年 Mac. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
-
-
- NSString *str1=@"abc";
- NSString *str2=@"Abc";
- NSString *str3=[NSString stringWithFormat:@"abc"];
- //isEqualToString 区分大小写 返回值bool类型
- if ([str1 isEqualToString:str2]) {
-
- NSLog(@"yes");
- }else{
-
- NSLog(@"no");
-
- }
-
-
- //compare 默认区分大小写 NSCaseInsensitiveSearch不区别大小写
- NSComparisonResult result=[str1 compare:str2 options:NSCaseInsensitiveSearch];
-
- switch (result) {
- case NSOrderedAscending:
- NSLog(@"a<b,升序");
- break;
- case NSOrderedDescending:
- NSLog(@"a>b,降序");
- break;
- case NSOrderedSame:
- NSLog(@"a==b,相等");
- break;
- default:
- break;
- }
-
- //hasPrefix 前缀检测 返回值为bool类型 [url hasPrefix:@"http://"]用方括号括起来
- NSString *url=@"http://www.baidu.com";
- if ([url hasPrefix:@"http://"]) {
- NSLog(@"这是一个网址");
- }else{
-
- NSLog(@"这不是一个网址");
- }
-
- //hasSuffix 后缀
- if ([url hasSuffix:@"du.com"]) {
- NSLog(@"这是一个网址");
- }else{
-
- NSLog(@"这不是一个网址");
- }
-
-
-
- NSString *str4=@"asdasdasdasdascatincsadadlajs;";
- NSString *str5=@"catin=";
- //查找子字符串返回首次出现位置 和长度
- NSRange range= [str4 rangeOfString:str5];
- // 查找不到range.location会返回 NSNotFound range.length返回为0
- if (range.location!=NSNotFound || range.length!=0) {
-
- NSLog(@"%lu\n%lu",range.location,range.length);
-
- }else{
-
- NSLog(@"没有找到....");
- }
-
-
-
-
- }
- return 0;
- }
复制代码
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
-
- NSString *str1=@"<strong>你 好* 世界 </strong>";
-
- //从》开始加1 也就是你开始
- NSUInteger loc=[str1 rangeOfString:@">"].location+1;
-
- NSUInteger len=[str1 rangeOfString:@"</"].location-loc;
-
- //动态传区间
- NSRange range={loc,len};
-
- NSString *str1New=[str1 substringWithRange:range];
- NSLog(@"str1New=%@\n",str1New);
-
- NSString *str11=[str1New stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
- NSString *str22=[str11 stringByReplacingOccurrencesOfString:@" " withString:@""];
- NSLog(@"%@\n",str22);
- //将*替换成/ 空格去掉
- NSString *str=@"http:** ww w. baidu. com";
-
- //stringByReplacingOccurrencesOfString 替换字符串
- NSString *strNew=[str stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
- NSLog(@"strNew=%@",strNew);
-
- strNew=[strNew stringByReplacingOccurrencesOfString:@" " withString:@""];
-
- NSLog(@"strNew=%@",strNew);
-
-
- NSString *str4=@" hellow word ";
- //去除首尾空格
- str4= [str4 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- NSLog(@"str4=%@",str4);
- }
-
- return 0;
- }
复制代码
|
|