zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【分享】IT产业中的三大定理(二) —— 安迪&比尔定理 (Andy and Bill's Law)
    【分享】IT产业中的三大定理(一) —— 摩尔定理(Moore's Law)
    Linux(Ubuntu)下也能用搜狗输入法了!!!
    聊聊付费软件
    JSP中使用Spring注入的Bean时需要注意的地方
    Spark (十二) Spark Streaming详解
    Kafka(一) 初识
    Spark (十一) spark使用hive的元数据信息
    Spark (十) Spark 的种类型Join
    Spark 学习(九) SparkSQL 函数自定义和数据源
  • 原文地址:https://www.cnblogs.com/niexiaobo/p/4643237.html
Copyright © 2011-2022 走看看