NSstring封装

来自http://devtang.com/blog/2012/02/14/nsstring-java-like-wrapper/

NSStringWrapper.h

#import <Foundation/Foundation.h>

@interface NSString(Wrapper)

/**  Return the char value at the specified index. */

- (unichar) charAt:(int)index;

/**

* Compares two strings lexicographically.

* the value 0 if the argument string is equal to this string;

* a value less than 0 if this string is lexicographically less than the string argument;

* and a value greater than 0 if this string is lexicographically greater than the string argument.

*/

- (int) compareTo:(NSString*) anotherString;

- (int) compareToIgnoreCase:(NSString*) str;

- (BOOL) contains:(NSString*) str;

- (BOOL) startsWith:(NSString*)prefix;

- (BOOL) endsWith:(NSString*)suffix;

- (BOOL) equals:(NSString*) anotherString;

- (BOOL) equalsIgnoreCase:(NSString*) anotherString;

- (int) indexOfChar:(unichar)ch;

- (int) indexOfChar:(unichar)ch fromIndex:(int)index;

- (int) indexOfString:(NSString*)str;

- (int) indexOfString:(NSString*)str fromIndex:(int)index;

- (int) lastIndexOfChar:(unichar)ch;

- (int) lastIndexOfChar:(unichar)ch fromIndex:(int)index;

- (int) lastIndexOfString:(NSString*)str;

- (int) lastIndexOfString:(NSString*)str fromIndex:(int)index;

- (NSString *) substringFromIndex:(int)beginIndex toIndex:(int)endIndex;

- (NSString *) toLowerCase;

- (NSString *) toUpperCase;

- (NSString *) trim;

- (NSString *) replaceAll:(NSString*)origin with:(NSString*)replacement;

- (NSArray *) split:(NSString*) separator;

@end

==============================================

NSStringWrapper.m

#import "NSStringWrapper.h"

@implementation NSString(Wrapper)

#define JavaNotFound -1

/**  Java-like method. Returns the char value at the specified index. */

- (unichar) charAt:(int)index {

return [self characterAtIndex:index];

}

/**

* Java-like method. Compares two strings lexicographically.

* the value 0 if the argument string is equal to this string;

* a value less than 0 if this string is lexicographically less than the string argument;

* and a value greater than 0 if this string is lexicographically greater than the string argument.

*/

- (int) compareTo:(NSString*) anotherString {

return [self compare:anotherString];

}

/** Java-like method. Compares two strings lexicographically, ignoring case differences. */

- (int) compareToIgnoreCase:(NSString*) str {

return [self compare:str options:NSCaseInsensitiveSearch];

}

/** Java-like method. Returns true if and only if this string contains the specified sequence of char values. */

- (BOOL) contains:(NSString*) str {

NSRange range = [self rangeOfString:str];

return (range.location != NSNotFound);

}

- (BOOL) startsWith:(NSString*)prefix {

return [self hasPrefix:prefix];

}

- (BOOL) endsWith:(NSString*)suffix {

return [self hasSuffix:suffix];

}

- (BOOL) equals:(NSString*) anotherString {

return [self isEqualToString:anotherString];

}

- (BOOL) equalsIgnoreCase:(NSString*) anotherString {

return [[self toLowerCase] equals:[anotherString toLowerCase]];

}

- (int) indexOfChar:(unichar)ch{

return [self indexOfChar:ch fromIndex:0];

}

- (int) indexOfChar:(unichar)ch fromIndex:(int)index{

int len = self.length;

for (int i = index; i < len; ++i) {

if (ch == [self charAt:i]) {

return i;

}

}

return JavaNotFound;

}

- (int) indexOfString:(NSString*)str {

NSRange range = [self rangeOfString:str];

if (range.location == NSNotFound) {

return JavaNotFound;

}

return range.location;

}

- (int) indexOfString:(NSString*)str fromIndex:(int)index {

NSRange fromRange = NSMakeRange(index, self.length - index);

NSRange range = [self rangeOfString:str options:NSLiteralSearch range:fromRange];

if (range.location == NSNotFound) {

return JavaNotFound;

}

return range.location;

}

- (int) lastIndexOfChar:(unichar)ch {

int len = self.length;

for (int i = len-1; i >=0; --i) {

if ([self charAt:i] == ch) {

return i;

}

}

return JavaNotFound;

}

- (int) lastIndexOfChar:(unichar)ch fromIndex:(int)index {

int len = self.length;

if (index >= len) {

index = len - 1;

}

for (int i = index; i >= 0; --i) {

if ([self charAt:i] == ch) {

return index;

}

}

return JavaNotFound;

}

- (int) lastIndexOfString:(NSString*)str {

NSRange range = [self rangeOfString:str options:NSBackwardsSearch];

if (range.location == NSNotFound) {

return JavaNotFound;

}

return range.location;

}

- (int) lastIndexOfString:(NSString*)str fromIndex:(int)index {

NSRange fromRange = NSMakeRange(0, index);

NSRange range = [self rangeOfString:str options:NSBackwardsSearch range:fromRange];

if (range.location == NSNotFound) {

return JavaNotFound;

}

return range.location;

}

- (NSString *) substringFromIndex:(int)beginIndex toIndex:(int)endIndex {

if (endIndex <= beginIndex) {

return @"";

}

NSRange range = NSMakeRange(beginIndex, endIndex - beginIndex);

return [self substringWithRange:range];

}

- (NSString *) toLowerCase {

return [self lowercaseString];

}

- (NSString *) toUpperCase {

return [self uppercaseString];

}

- (NSString *) trim {

return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

}

- (NSString *) replaceAll:(NSString*)origin with:(NSString*)replacement {

return [self stringByReplacingOccurrencesOfString:origin withString:replacement];

}

- (NSArray *) split:(NSString*) separator {

return [self componentsSeparatedByString:separator];

}

@end

时间: 2024-11-09 13:28:08

NSstring封装的相关文章

IOS NSURLRequest(http请求)讲解 ---------赎罪之路

前言 很久没有写过博客了,今天终于可以抽空写.公司的项目从2016年03月15日(我第二份工作任职)开始,辛苦了3个多月终于接近尾声了,在这当中我学了非常多东西,为了遗忘我就写个博客来记录下,以防止忘记. 正文 今天要讲的主角是NSURLRequest.这里我先采用apple 官方文档解释 NSURLRequest objects represent a URL load request in a manner independent of protocol and URL scheme, NS

Objective-C精选字符串处理方法

无论是什么编程语言对字符串的操作是少不了的,对复杂的字符串的分析和操作我们可以用正则表达式来达到我们的目的.简单的字符串处理我们可以借助OC中NSString封装好的字符串处理方法,不过前提是你得了解每个方法如何使用,以下做了一个简单的总结,便于以后使用.以下事例是原创代码,转载请注明出处. 1.字符串的创建 字符串的创建有多种方法,下面的例子中给了常用的几种字符串创建方法(还有好多) 1 2 3 4 5 6     //字符串创建工厂     NSString *str1 = [NSStrin

三.OC基础--1.NSString的创建和使用,2多文件开发,3类方法,4封装

三:OC--1.NSString的创建和使用, 1,创建常量字符串,注意使用“@“符号. NSString *astring = @"This is a String!"; //后面的是常量,前面的是指针变量 2,NSString *s1 = [NSString new];//没有意义 s1 [email protected]"jian le ma"; 3.创建字符串的第三种方法 // 格式化创建字符串(按照指定的格式创建字符串) NSString *imgName

[封装] 修改NSString中部分字段的颜色

- (NSMutableAttributedString *)adjustTextColor:(NSString *)text rangeText:(NSString *)rangeText color:(UIColor *)color { NSRange range = [text rangeOfString:rangeText]; NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWit

黑马程序-oc基础---封装 继承 多态 NSString 代码规范格式的重要性

//写代码的一条重要性:就是代码编写规范,尤其是xcode编写对格式的对齐有关系,在下面的代码没有怎么对齐的情况下出现30个警告 //一般oc程序的头文件 #import <Foundation/Foundation.h> typedef enum { SexMan, SexWoman } Sex; //类的声明 @interface Student : NSObject { int _age; // int no; Sex _sex; } -(void)setAge:(int)newAge;

UISCrollView —— 图片轮播器封装实现(三)——(第三讲)

1.分析 利用xib布局,然后自定义一个UIView,解析xib,然后利用控制器传入数据,将其加载到控制器的view上展示即可 2. 程序结构 3. 代码具体实现 1>  xib文件 2>  创建类 XMGPageView,然后将其与xib文件关联,选中xib文件,然后设置下文中 " custom class " 为定义的类,让其来管理xib 1>    (如图,设置xib与类关联) 2>  XMGPageView.h 文件中,声明分页图片数组属性,及其一个快速

第八篇、封装NSURLSession网络请求框架

主要功能介绍: 1.GET请求操作 2.POST请求操作 1.处理params参数(例如拼接成:usename="123"&password="123") #import <Foundation/Foundation.h> @interface LYURLRequestSerialization : NSObject +(NSString *)LYQueryStringFromParameters:(NSDictionary *)paramete

Objective--C三大特性:封装,继承,多态(杂乱的东西)

OC中类的特性共有有三个,它们分别是封装.多态和继承. 1.成员变量的作用域.成员变量的作用域应该也是封装的一种,它对成员变量的作用范围做出了一些限制. @public : 在任何地方都可以直接访问成员变量. @protected:在本类和子类中可以直接访问. @private:只能在本类中访问. @package:在同一个框架内可以访问. 默认情况下在类的声明中定义的成员变量的修饰符是@protected,在类的实现中定义的成员变量的修饰符是@private. 总结:(1)封装是为了保护类中的

PCM转换MP3的工具封装

说明 1. 对 PCM 转 MP3 进行了简单的封装. 2. 使用 https://github.com/wuqiong/mp3lame-for-iOS 生成支持64位的 lame 库. 源码 https://github.com/YouXianMing/iOS-General-Tools 中的 PCM-to-MP3 // // PcmToMp3Manager.h // RecordMusic // // Created by YouXianMing on 16/7/28. // Copyrig