- #import "NSString+extend.h"
- @implementation NSString(extend)
- //设计为类方法,字符串反转.
- +(NSString*)fanZhuan:(NSString*)str
- {
- //创建一个空的可变字符串
- NSMutableString *result=[NSMutableString string];
- //倒叙读取原始字符串
- for(int i=(int)str.length-1;i>=0;i--)
- {
- char c=[str characterAtIndex:i];
- [result appendFormat:@"%c",c];//可变字符串依次加上
- }
- return result;
- };
- //计算英文字母的个数
- +(int)englishCount:(NSString*)str
- {
- int count=0;
- for(int i=0;i<str.length;i++)
- {
- char ch=[str characterAtIndex:i];
- //如果取出来的那个字符在a-z之间或A-Z之间,结果加1
- if((ch-'a'>=0&&ch-'z'<=0)||(ch-'A'>=0&&ch-'Z'<=0))
- count++;
- }
- return count;
- };
- //去除字符串两端的空格.
- +(NSString*)quChuKongGe:(NSString*)str
- {
- int start=0,end=(int)str.length-1;
- //找到从前往后数第一个非空格的位置
- while(1)
- {
- char ch=[str characterAtIndex:start];
- if(ch==' ')
- start++;
- else
- break;
- }
- while(1)
- {
- char ch=[str characterAtIndex:end];
- if(ch==' ')
- end--;
- else
- break;
- }
- //截取从start到end的字符串,即为结果
- //NSLog(@"%d %d 长度为%d",start,end,str.length);
- return [str substringWithRange:NSMakeRange(start, end-start+1)];
- };
- @end
复制代码 |