OC에서 자주 사용하는 String 범주
+ (instancetype)stringWithUrlStr:(NSString *)webaddress WithKey:(NSString *)CS
{
NSError *error;
NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",CS];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:webaddress
options:0
range:NSMakeRange(0, [webaddress length])];
for (NSTextCheckingResult *match in matches) {
NSString *tagValue = [webaddress substringWithRange:[match rangeAtIndex:2]];
return tagValue;
}
return nil;
}
문자열에 중국어가 있는지 확인하기
+ (BOOL)stringIsContainChineseWithStr:(NSString *)str
{
for(int i=0; i< [str length];i++){
int a = [str characterAtIndex:i];
if( a > 0x4e00 && a < 0x9fff) {
return YES;
}
}
return NO;
}
QQ번호 확인
- (BOOL)stringIsQQNumber
{
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]{5,15}$" options:0 error:nil];
if (regex) {
NSTextCheckingResult * resulte = [regex firstMatchInString:self options:0 range:NSMakeRange(0, self.length)];
if (resulte) {
return YES;
}
else
{
return NO;
}
}
return NO;
}
핸드폰 번호인지 아닌지를 판단하다
- (BOOL)stringIsPhoneNumber
{
//
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"^[1][358][0-9]{9}" options:0 error:nil];
if (regex) {
NSTextCheckingResult * resulte = [regex firstMatchInString:self options:0 range:NSMakeRange(0,self.length)];
if (resulte) {
return YES;
}
else
{
return NO;
}
}
return NO;
}
숫자
- (BOOL)stringIsNumber
{
//
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:0 error:nil];
if (regex) {
NSTextCheckingResult * resulte = [regex firstMatchInString:self options:0 range:NSMakeRange(0, self.length)];
if (resulte) {
return YES;
}
else
{
return NO;
}
}
return NO;
}
주민등록번호
+ (BOOL)stringIsCardNumber:(NSString *)value
{
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([value length] != 18) {
return NO;
}
NSString *mmdd = @"(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))";
NSString *leapMmdd = @"0229";
NSString *year = @"(19|20)[0-9]{2}";
NSString *leapYear = @"(19|20)(0[48]|[2468][048]|[13579][26])";
NSString *yearMmdd = [NSString stringWithFormat:@"%@%@", year, mmdd];
NSString *leapyearMmdd = [NSString stringWithFormat:@"%@%@", leapYear, leapMmdd];
NSString *yyyyMmdd = [NSString stringWithFormat:@"((%@)|(%@)|(%@))", yearMmdd, leapyearMmdd, @"20000229"];
NSString *area = @"(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}";
NSString *regex = [NSString stringWithFormat:@"%@%@%@", area, yyyyMmdd , @"[0-9]{3}[0-9Xx]"];
NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if (![regexTest evaluateWithObject:value]) {
return NO;
}
int summary = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7
+ ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9
+ ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10
+ ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5
+ ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8
+ ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4
+ ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2
+ [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6
+ [value substringWithRange:NSMakeRange(9,1)].intValue * 3;
NSInteger remainder = summary % 11;
NSString *checkBit = @"";
NSString *checkString = @"10X98765432";
checkBit = [checkString substringWithRange:NSMakeRange(remainder,1)];//
return [checkBit isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]];
}
신분증 번호에 따라 연령과 성별을 얻다
+ (instancetype)stringGetAgeAndSexWithCardID:(NSString *)cardID
{
NSString *sex, *age;
NSString * ageString = [cardID substringWithRange:NSMakeRange(6, 4)];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY"];
NSString *year = [formatter stringFromDate:[NSDate date]];
int nowYear = [year intValue];
if ([cardID length] == 18) {
NSString * sexString = [cardID substringWithRange:NSMakeRange(16, 1)];
age = [NSString stringWithFormat:@"%d", nowYear - [ageString intValue]];
if ([sexString intValue]%2 == 0) {
sex = @" ";
}else{
sex = @" ";
}
}else if ([cardID length] == 15){
NSString * ageString = [cardID substringWithRange:NSMakeRange(6, 2)];
age = [NSString stringWithFormat:@"%d", nowYear - [[NSString stringWithFormat:@"19%d",[ageString intValue]]intValue] ];
NSString * sexString = [cardID substringWithRange:NSMakeRange(13, 1)];
if ([sexString intValue]%2 == 0) {
sex = @" ";
}else{
sex = @" ";
}
}
return [age stringByAppendingString:sex];
}
MD5 암호화
+ (instancetype)stringWithMD5:(NSString *)str
{
const char *cStr = [str UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (unsigned int)strlen(cStr), digest );
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02X", digest[i]];
return output;
}
sha1 암호화
+ (instancetype)stringWithsha1:(NSString *)str
{
const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:str.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (unsigned int)data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}
IP 확보
+ (instancetype)stringWithIP
{
NSString *address = @"an error occurred when obtaining ip address";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
success = getifaddrs(&interfaces);
if (success == 0) { // 0
temp_addr = interfaces;
while (temp_addr != NULL) {
if( temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
return address;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.