NSString(FWEncode)

@interface NSString (FWEncode)

#pragma mark - Json

/**
 Foundation对象编码为json字符串
 
 @param object 编码对象
 @return json字符串
 */
+ (nullable NSString *)fw_jsonEncode:(id)object NS_REFINED_FOR_SWIFT;

/**
 *  json字符串解码为Foundation对象
 *
 *  @return Foundation对象
 */
- (nullable id)fw_jsonDecode NS_REFINED_FOR_SWIFT;

#pragma mark - Base64

/**
 *  base64编码
 *
 *  @return base64字符串
 */
- (nullable NSString *)fw_base64Encode NS_REFINED_FOR_SWIFT;

/**
 *  base64解码
 *
 *  @return 原字符串
 */
- (nullable NSString *)fw_base64Decode NS_REFINED_FOR_SWIFT;

#pragma mark - Unicode

/**
 *  计算长度,中文为1,英文为0.5
 */
- (NSUInteger)fw_unicodeLength NS_REFINED_FOR_SWIFT;

/**
 *  截取字符串,中文为1,英文为0.5
 *
 *  @param length 截取长度
 */
- (NSString *)fw_unicodeSubstring:(NSUInteger)length NS_REFINED_FOR_SWIFT;

/**
 *  Unicode中文编码,将中文转换成Unicode字符串(如\u7E8C)
 *
 *  @return Unicode字符串
 */
- (NSString *)fw_unicodeEncode NS_REFINED_FOR_SWIFT;

/**
 *  Unicode中文解码,将Unicode字符串(如\u7E8C)转换成中文
 *
 *  @return 中文字符串
 */
- (NSString *)fw_unicodeDecode NS_REFINED_FOR_SWIFT;

#pragma mark - Url

/**
 *  url参数编码,适用于query参数编码
 *  示例:http://test.com?id=我是中文 =>
 *       http%3A%2F%2Ftest.com%3Fid%3D%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87
 *
 *  @return url编码字符串
 */
- (nullable NSString *)fw_urlEncodeComponent NS_REFINED_FOR_SWIFT;

/**
 *  url参数解码,适用于query参数解码
 *  示例:http%3A%2F%2Ftest.com%3Fid%3D%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87 =>
 *       http://test.com?id=我是中文
 *
 *  @return 原字符串
 */
- (nullable NSString *)fw_urlDecodeComponent NS_REFINED_FOR_SWIFT;

/**
 *  url编码,适用于整个url编码
 *  示例:http://test.com?id=我是中文 =>
 *       http://test.com?id=%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87
 *
 *  @return url编码地址
 */
- (nullable NSString *)fw_urlEncode NS_REFINED_FOR_SWIFT;

/**
 *  url解码,适用于整个url解码
 *  示例:http://test.com?id=%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87 =>
 *       http://test.com?id=我是中文
 *
 *  @return 原url地址
 */
- (nullable NSString *)fw_urlDecode NS_REFINED_FOR_SWIFT;

#pragma mark - Query

/**
 * 字典编码为URL参数字符串
 */
+ (NSString *)fw_queryEncode:(NSDictionary<NSString *, id> *)dictionary NS_REFINED_FOR_SWIFT;

/**
 * URL参数字符串解码为字典,支持完整URL
 */
- (NSDictionary<NSString *, NSString *> *)fw_queryDecode NS_REFINED_FOR_SWIFT;

#pragma mark - Md5

/**
 *  md5编码
 *
 *  @return md5字符串
 */
- (NSString *)fw_md5Encode NS_REFINED_FOR_SWIFT;

/**
 *  文件md5编码
 *
 *  @return md5字符串
 */
- (nullable NSString *)fw_md5EncodeFile NS_REFINED_FOR_SWIFT;

#pragma mark - Helper

/**
 去掉空白字符
 */
@property (nonatomic, copy, readonly) NSString *fw_trimString NS_REFINED_FOR_SWIFT;

/**
 首字母大写
 */
@property (nonatomic, copy, readonly) NSString *fw_ucfirstString NS_REFINED_FOR_SWIFT;

/**
 首字母小写
 */
@property (nonatomic, copy, readonly) NSString *fw_lcfirstString NS_REFINED_FOR_SWIFT;

/**
 驼峰转下划线
 */
@property (nonatomic, copy, readonly) NSString *fw_underlineString NS_REFINED_FOR_SWIFT;

/**
 下划线转驼峰
 */
@property (nonatomic, copy, readonly) NSString *fw_camelString NS_REFINED_FOR_SWIFT;

/**
 中文转拼音
 */
@property (nonatomic, copy, readonly) NSString *fw_pinyinString NS_REFINED_FOR_SWIFT;

/**
 中文转拼音并进行比较
 */
- (NSComparisonResult)fw_pinyinCompare:(NSString *)string NS_REFINED_FOR_SWIFT;

/**
 过滤JSON解码特殊字符
 
 兼容\uD800-\uDFFF引起JSON解码报错3840问题,不报错时无需调用
 规则:只允许以\uD800-\uDBFF高位开头,紧跟\uDC00-\uDFFF低位;其他全不允许
 参考:https://github.com/SBJson/SBJson/blob/trunk/Classes/SBJson5StreamTokeniser.m
 */
@property (nonatomic, copy, readonly) NSString *fw_escapeJson NS_REFINED_FOR_SWIFT;

/**
 转换为UTF8编码数据
 */
@property (nonatomic, strong, readonly, nullable) NSData *fw_utf8Data NS_REFINED_FOR_SWIFT;

/**
 转换为NSURL
 */
@property (nonatomic, copy, readonly, nullable) NSURL *fw_url NS_REFINED_FOR_SWIFT;

/**
 转换为NSNumber
 */
@property (nonatomic, readonly, nullable) NSNumber *fw_number NS_REFINED_FOR_SWIFT;

@end

Undocumented

Json

  • Foundation对象编码为json字符串

    Declaration

    Objective-C

    + (nullable NSString *)fw_jsonEncode:(nonnull id)object;

    Parameters

    object

    编码对象

    Return Value

    json字符串

  • json字符串解码为Foundation对象

    Declaration

    Objective-C

    - (nullable id)fw_jsonDecode;

    Return Value

    Foundation对象

Base64

  • base64编码

    Declaration

    Objective-C

    - (nullable NSString *)fw_base64Encode;

    Return Value

    base64字符串

  • base64解码

    Declaration

    Objective-C

    - (nullable NSString *)fw_base64Decode;

    Return Value

    原字符串

Unicode

  • 计算长度,中文为1,英文为0.5

    Declaration

    Objective-C

    - (NSUInteger)fw_unicodeLength;
  • 截取字符串,中文为1,英文为0.5

    Declaration

    Objective-C

    - (nonnull NSString *)fw_unicodeSubstring:(NSUInteger)length;

    Parameters

    length

    截取长度

  • Unicode中文编码,将中文转换成Unicode字符串(如\u7E8C)

    Declaration

    Objective-C

    - (nonnull NSString *)fw_unicodeEncode;

    Return Value

    Unicode字符串

  • Unicode中文解码,将Unicode字符串(如\u7E8C)转换成中文

    Declaration

    Objective-C

    - (nonnull NSString *)fw_unicodeDecode;

    Return Value

    中文字符串

Url

Query

  • 字典编码为URL参数字符串

    Declaration

    Objective-C

    + (nonnull NSString *)fw_queryEncode:
        (nonnull NSDictionary<NSString *, id> *)dictionary;
  • URL参数字符串解码为字典,支持完整URL

    Declaration

    Objective-C

    - (nonnull NSDictionary<NSString *, NSString *> *)fw_queryDecode;

Md5

  • md5编码

    Declaration

    Objective-C

    - (nonnull NSString *)fw_md5Encode;

    Return Value

    md5字符串

  • 文件md5编码

    Declaration

    Objective-C

    - (nullable NSString *)fw_md5EncodeFile;

    Return Value

    md5字符串

Helper

  • 去掉空白字符

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NS_REFINED_FOR_SWIFT NSString *fw_trimString;
  • 首字母大写

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NS_REFINED_FOR_SWIFT NSString *fw_ucfirstString;
  • 首字母小写

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NS_REFINED_FOR_SWIFT NSString *fw_lcfirstString;
  • 驼峰转下划线

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NS_REFINED_FOR_SWIFT NSString *fw_underlineString;
  • 下划线转驼峰

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NS_REFINED_FOR_SWIFT NSString *fw_camelString;
  • 中文转拼音

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NS_REFINED_FOR_SWIFT NSString *fw_pinyinString;
  • 中文转拼音并进行比较

    Declaration

    Objective-C

    - (NSComparisonResult)fw_pinyinCompare:(nonnull NSString *)string;
  • 过滤JSON解码特殊字符

    兼容\uD800-\uDFFF引起JSON解码报错3840问题,不报错时无需调用 规则:只允许以\uD800-\uDBFF高位开头,紧跟\uDC00-\uDFFF低位;其他全不允许 参考:https://github.com/SBJson/SBJson/blob/trunk/Classes/SBJson5StreamTokeniser.m

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NS_REFINED_FOR_SWIFT NSString *fw_escapeJson;
  • 转换为UTF8编码数据

    Declaration

    Objective-C

    @property (nonatomic, strong, readonly, nullable) NSData *fw_utf8Data;
  • 转换为NSURL

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly, nullable) NSURL *fw_url;
  • 转换为NSNumber

    Declaration

    Objective-C

    @property (nonatomic, readonly, nullable) NSNumber *fw_number;

NSString+FWSafeType

  • 从指定位置截取子串

    Declaration

    Objective-C

    - (nullable NSString *)fw_substringFromIndex:(NSInteger)from;

    Parameters

    from

    起始位置

    Return Value

    子串

  • 截取子串到指定位置

    Declaration

    Objective-C

    - (nullable NSString *)fw_substringToIndex:(NSInteger)to;

    Parameters

    to

    结束位置

    Return Value

    子串

  • 截取指定范围的子串

    Declaration

    Objective-C

    - (nullable NSString *)fw_substringWithRange:(NSRange)range;

    Parameters

    range

    指定范围

    Return Value

    子串

NSString+FWFoundation

  • 计算单行字符串指定字体所占尺寸

    Declaration

    Objective-C

    - (CGSize)fw_sizeWithFont:(nonnull UIFont *)font;
  • 计算多行字符串指定字体在指定绘制区域内所占尺寸

    Declaration

    Objective-C

    - (CGSize)fw_sizeWithFont:(nonnull UIFont *)font drawSize:(CGSize)drawSize;
  • 计算多行字符串指定字体、指定属性在指定绘制区域内所占尺寸

    Declaration

    Objective-C

    - (CGSize)fw_sizeWithFont:(nonnull UIFont *)font
                     drawSize:(CGSize)drawSize
                   attributes:(nullable NSDictionary<NSAttributedStringKey, id> *)
                                  attributes;
  • 是否匹配正则表达式,示例:^[a-zA-Z0-9_\u4e00-\u9fa5]{4,14}$

    Declaration

    Objective-C

    - (BOOL)fw_matchesRegex:(nonnull NSString *)regex;
  • 格式化文件大小为".0K/.1M/.1G"

    Declaration

    Objective-C

    + (nonnull NSString *)fw_sizeString:(NSUInteger)fileSize;
  • 安全截取字符串。解决末尾半个Emoji问题(半个Emoji调UTF8String为NULL,导致MD5签名等失败)

    Declaration

    Objective-C

    - (nonnull NSString *)fw_emojiSubstring:(NSUInteger)index;

    Parameters

    index

    目标索引

  • 正则搜索子串

    Declaration

    Objective-C

    - (nullable NSString *)fw_regexSubstring:(nonnull NSString *)regex;

    Parameters

    regex

    正则表达式

  • 正则替换字符串

    Declaration

    Objective-C

    - (nonnull NSString *)fw_regexReplace:(nonnull NSString *)regex
                               withString:(nonnull NSString *)string;

    Parameters

    regex

    正则表达式

    string

    替换模板,如"头部$1中部$2尾部“

    Return Value

    替换后的字符串

  • 正则匹配回调

    Declaration

    Objective-C

    - (void)fw_regexMatches:(nonnull NSString *)regex
                  withBlock:(nonnull void (^)(NSRange))block;

    Parameters

    regex

    正则表达式

    block

    回调句柄。range从大至小,方便replace

  • 转义Html,如"a<“转义为"a<”

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NS_REFINED_FOR_SWIFT NSString *fw_escapeHtml;
  • 是否符合正则表达式 示例:用户名:^[a-zA-Z][a-zA-Z0-9_]{4,13}$ 密码:^[a-zA-Z0-9_]{6,20}$ 昵称:^[a-zA-Z0-9_\u4e00-\u9fa5]{4,14}$

    Declaration

    Objective-C

    - (BOOL)fw_isFormatRegex:(nonnull NSString *)regex;

    Parameters

    regex

    正则表达式

  • 是否是手机号

    Declaration

    Objective-C

    - (BOOL)fw_isFormatMobile;
  • 是否是座机号

    Declaration

    Objective-C

    - (BOOL)fw_isFormatTelephone;
  • 是否是整数

    Declaration

    Objective-C

    - (BOOL)fw_isFormatInteger;
  • 是否是数字

    Declaration

    Objective-C

    - (BOOL)fw_isFormatNumber;
  • 是否是合法金额,两位小数点

    Declaration

    Objective-C

    - (BOOL)fw_isFormatMoney;
  • 是否是身份证号

    Declaration

    Objective-C

    - (BOOL)fw_isFormatIdcard;
  • 是否是银行卡号

    Declaration

    Objective-C

    - (BOOL)fw_isFormatBankcard;
  • 是否是车牌号

    Declaration

    Objective-C

    - (BOOL)fw_isFormatCarno;
  • 是否是邮政编码

    Declaration

    Objective-C

    - (BOOL)fw_isFormatPostcode;
  • 是否是邮箱

    Declaration

    Objective-C

    - (BOOL)fw_isFormatEmail;
  • 是否是URL

    Declaration

    Objective-C

    - (BOOL)fw_isFormatUrl;
  • 是否是HTML

    Declaration

    Objective-C

    - (BOOL)fw_isFormatHtml;
  • 是否是IP

    Declaration

    Objective-C

    - (BOOL)fw_isFormatIp;
  • 是否全是中文

    Declaration

    Objective-C

    - (BOOL)fw_isFormatChinese;
  • 是否是合法时间,格式:yyyy-MM-dd HH:mm:ss

    Declaration

    Objective-C

    - (BOOL)fw_isFormatDatetime;
  • 是否是合法时间戳,格式:1301234567

    Declaration

    Objective-C

    - (BOOL)fw_isFormatTimestamp;
  • 是否是坐标点字符串,格式:latitude,longitude

    Declaration

    Objective-C

    - (BOOL)fw_isFormatCoordinate;

NSString+FWLanguage

  • 快速读取本地化语言

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NS_REFINED_FOR_SWIFT NSString *fw_localized;