因项目需求,微信呢称中需要去掉特殊。直接上代码了。希望对大家有帮助
ios版:
const char* IosHelper::removeEmoji(const char* str){ std::string strLen = str; Nsstring* username = [[Nsstring alloc] initWithBytes:str length:strLen.length() enCoding:NSUTF8StringEnCoding]; cocos2d::log("IosHelper::removeEmoji username0 = %s",str); NSLog(@"IosHelper::removeEmoji username1 = %@",username); //Nsstring *regex = @"^[a-zA-Z0-9_\u4e00-\u9fa5]+$"; Nsstring *regex = @"[\u0020-\u007e\u4e00-\u9fa5]"; //Nsstring *regex = @"^[a-zA-Z\u4e00-\u9fa5]+"; nspredicate *predicate = [nspredicate predicateWithFormat:@"SELF MATCHES %@",regex]; Nsstring *temp = nil; //NSLog(@"IosHelper::removeEmoji username = %d",[username length]); for(int i = 0; i < [username length]; i++) { temp = [username substringWithRange:NSMakeRange(i,1)]; //NSLog(@"i = %d char = %x str = %@",i,temp,temp); if ([predicate evaluateWithObject:temp]) { //NSLog(@"This character is OK"); } else { NSRange range = NSMakeRange(i,1); username = [username stringByReplacingCharactersInRange:range withString:@" "]; } } Nsstring *withoutEmojiUsername = [username stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"IosHelper::removeEmoji username2 = %@",withoutEmojiUsername); const char* retStr = [withoutEmojiUsername UTF8String]; cocos2d::log("IosHelper::removeEmoji username3 = %s",retStr); return retStr;}androID
public static String RemoveEmoji(String str) { //String regEx = "[^A-Za-z0-9\u4e00-\u9fa5]"; String regEx = "[^\u0020-\u007e\u4e00-\u9fa5]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll("").trim(); }总结
以上是内存溢出为你收集整理的cocos2d中移除微信昵称中的特殊字符(ios与android)全部内容,希望文章能够帮你解决cocos2d中移除微信昵称中的特殊字符(ios与android)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)