1,當(dāng)鍵盤(pán)為中文輸入法時(shí),移除聯(lián)想時(shí)中間的空格字符
<pre><code>
正則表達(dá)式 \w \s \d \b 2012-09-19 22:21:49
分類(lèi): Python/Ruby
正則表達(dá)式 \w \s \d \b
. 匹配除換行符以外的任意字符
\w 匹配字母或數(shù)字或下劃線或漢字 等價(jià)于 '[^A-Za-z0-9_]'塌计。
\s 匹配任意的空白符
\d 匹配數(shù)字
\b 匹配單詞的開(kāi)始或結(jié)束
^ 匹配字符串的開(kāi)始
$ 匹配字符串的結(jié)束
\w能不能匹配漢字要視你的操作系統(tǒng)和你的應(yīng)用環(huán)境而定
</code></pre>
<pre><code>
- (NSString *) removeWhitespace
{
return [self stringByReplacingOccurrencesOfString:@"\s" withString:@""
options:NSRegularExpressionSearch
range:NSMakeRange(0, [self length])];
}
</code></pre>
2,過(guò)濾Emoji
<pre><code>
-
(NSString*)removeEmojiWithInputString :(NSString )str {
__block NSMutableString temp = [NSMutableString string];[str enumerateSubstringsInRange: NSMakeRange(0, str.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){const unichar hs = [substring characterAtIndex: 0]; // surrogate pair if (0xd800 <= hs && hs <= 0xdbff) { const unichar ls = [substring characterAtIndex: 1]; const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000; [temp appendString: (0x1d000 <= uc && uc <= 0x1f77f)? @"": substring]; // U+1D000-1F77F // non surrogate } else { [temp appendString: (0x2100 <= hs && hs <= 0x26ff)? @"": substring]; // U+2100-26FF }
}];
return temp;
}
</code></pre>