ios NSString具有前缀:区分大小写函数

vh0rcniy  于 2023-02-26  发布在  iOS
关注(0)|答案(8)|浏览(207)

很简单,可能会问很多次,但这是一个小技巧。我们知道,NSString不区分hasPrefix的大小写:方法。

NSString *string = @"Xyzabcdedfghij";
    NSString *substring = @"xyz";
    if ([string hasPrefix:substring])
        NSLog(@"string has prefix "); // won't get here.

Question is:是否有任何内置的方法来解决这个问题?我的意思是,hasPrefix:区分大小写?
我可以用下面的答案至少一个例子。但是想知道有没有比这个更好的方法...?

    • 已知答案:**(租赁案例)
if ([[test substringWithRange:NSMakeRange(0,3)] caseInsensitiveCompare:@"xyz"] == NSOrderedSame) {
     // ....
}
shstlldc

shstlldc1#

Apple自身:

NSString *searchString = @"age";

NSString *beginsTest = @"Agencies";
NSRange prefixRange = [beginsTest rangeOfString:searchString
    options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];

// prefixRange = {0, 3}

NSString *endsTest = @"BRICOLAGE";
NSRange suffixRange = [endsTest rangeOfString:searchString
    options:(NSAnchoredSearch | NSCaseInsensitiveSearch | NSBackwardsSearch)];

// suffixRange = {6, 3}

这可以 Package 成一个易于使用的方法:

- (BOOL) string:(NSString *)string
      hasPrefix:(NSString *)prefix
caseInsensitive:(BOOL)caseInsensitive {

    if (!caseInsensitive)
        return [string hasPrefix:prefix];

    const NSStringCompareOptions options = NSAnchoredSearch|NSCaseInsensitiveSearch;
    NSRange prefixRange = [string rangeOfString:prefix
                                        options:options];
    return prefixRange.location == 0 && prefixRange.length > 0;
}
nfs0ujit

nfs0ujit2#

你总是可以在两个字符串上使用lowercaseString,这样就强制相同的大小写。

[[string lowercaseString] hasPrefix:[substring lowercaseString]];
wrrgggsh

wrrgggsh3#

最糟糕的做法是将字符串和都小写,而不是使用hasPrefix。

[[mainString lowercaseString] hasPrefix:[stringToFind lowercaseString]];
qzwqbdag

qzwqbdag4#

雨燕5:

extension String {
    public func hasPrefixIgnoringCase(_ prefix: String) -> Bool {
        range(of: prefix, options: [.anchored, .caseInsensitive]) != nil
    }
}

用法:

"Hello".hasPrefixIgnoringCase("hEl") // return true
wtlkbnrh

wtlkbnrh5#

你可以通过

if([[string lowercaseString] hasPrefix:[substring lowercaseString]])
{
NSLog(@"found");
}
aurhwmvo

aurhwmvo6#

Swift 4.2版本的已接受答案:

extension String {

    public func hasPrefix<Prefix>(_ prefix: Prefix, caseSensitive: Bool) -> Bool where Prefix : StringProtocol {

        if caseSensitive { return self.hasPrefix(prefix) }

        let prefixRange = self.range(of: prefix, options: [.anchored, .caseInsensitive])
        return prefixRange != nil
    }
}

或者,使用.lowercased()

extension String {

    public func hasPrefix<Prefix>(_ prefix: Prefix, caseSensitive: Bool) -> Bool where Prefix : StringProtocol {

        if caseSensitive { return self.hasPrefix(prefix) }
        return self.lowercased().hasPrefix(prefix.lowercased())
    }
}

该扩展将按如下方式使用

let string = "Hello World"

let caseSensitiveSearch = string.hasPrefix("hello", caseSensitive: true) // return false
let caseNotSensitiveSearch = string.hasPrefix("hello", caseSensitive: false) // return true
a0x5cqrl

a0x5cqrl7#

对于那些喜欢函数式一行程序的人(像我一样):

extension String {

    func hasPrefix<Prefix>(_ prefix: Prefix, caseSensitive: Bool) -> Bool where Prefix: StringProtocol {
        return caseSensitive ? hasPrefix(prefix) :
            self.range(of: prefix, options: [.anchored, .caseInsensitive]) != nil
    }

}
o4tp2gmn

o4tp2gmn8#

雨燕5

extension StringProtocol {
    public func hasPrefix<T: StringProtocol>(caseInsensitive prefix: T) -> Bool {
        lowercased().starts(with: prefix.lowercased())
    }
}

用途:

"Hello World".hasPrefix(caseInsensitive: "hello") //true

相关问题